No, one of the great advantages of Promises is that you you can keep your async code linear rather than nested (callback hell from continuation passing style).
Promises give you return statements and error throwing, which you lose with continuation passing style.
You need to return the promise from your async functions so you can chain on the returned value.
Here's an example:
driver.get('https://website.com/login')
.then(function() {
return loginPage.login('company.admin', 'password')
})
.then(function() {
var employeePage = new EmployeePage(driver.getDriver());
return employeePage.clickAddEmployee();
})
.then(function() {
setTimeout(function() {
var addEmployeeForm = new AddEmployeeForm(driver.getDriver());
addEmployeeForm.insertUserName(employee.username)
.then(function() {
return addEmployeeForm.insertFirstName(employee.firstName)
})
.then(function() {
return addEmployeeForm.insertLastName(employee.lastName)
})
.then(function() {
return addEmployeeForm.clickCreateEmployee()
})
.then(function() {
return employeePage.searchEmployee(employee)
});
}, 750);
});
Promise.all
takes an array of promises and resolves once all promises resolve, if any are rejected, the array is rejected. This allows you to execute async code concurrently rather than serially, and still wait for the result of all concurrent functions. If you're comfortable with a threaded model, think spawning threads and then joining.
Example:
addEmployeeForm.insertUserName(employee.username)
.then(function() {
// these two functions will be invoked immediately and resolve concurrently
return Promise.all([
addEmployeeForm.insertFirstName(employee.firstName),
addEmployeeForm.insertLastName(employee.lastName)
])
})
// this will be invoked after both insertFirstName and insertLastName have succeeded
.then(function() {
return addEmployeeForm.clickCreateEmployee()
})
.then(function() {
return employeePage.searchEmployee(employee)
})
// if an error arises anywhere in the chain this function will be invoked
.catch(function(err){
console.log(err)
});
Promise.resolve()
and Promise.reject()
are methods used when creating a Promise
. They're used to wrap an async function using callbacks so that you can work with Promises instead of callbacks.
Resolve will resolve/fulfill the promise (this means a chained then
method will be called with the resulting value).
Reject will reject the promise (this means any chained then
method(s) will not be called, but the first chained catch
method will be called with the error that arose).
I left your setTimeout
in place to preserve your programs behavior, but it's likely unnecessary.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…