const promise = new Promise((resolve, reject) => {
if(allWentWell) {
resolve('All things went well!');
} else {
reject('Something went wrong');
}
});
Can I change resolve
to res
? Or does resolve have special meaning here?
No they aren't keywords you can name them whatever you want, but IMO you should keep the names as descriptive as possible.
const promise = new Promise((res, rej) => { if(true) { res('All things went well;'); } else { rej('Something went wrong'); } }). promise.then(val=> console.log(val))
No. The Promise
constructor expects a function and will pass two arguments to it: the first one being a function that resolves the promise, and the second one being a function that rejects the promise. With that being said, you can name these arguments anything you want, but they must be in the correct order.
const myFunction = function (x, y) {
if (allWentWell) {
x('All things went well!')
} else {
y('Something went wrong')
}
}
new Promise(myFunction)
I'd say it's a good practice though to keep them named resolve
and reject
, as most examples on the web use it so programmers end up expecting for it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.