Consider this code:
function foo() {
return promiseFoo.then((res) => {
//...
//Construct a `promiseBar` object which is promise-like,
//but also has other uses.
//...
return promiseBar;
});
}
foo().then((res) => {
//Because `promiseBar` is promise-like, it was automatically resolved.
//So now, `res` is, for example, a primitive string instead of the
//original `promiseBar` object
})
Is there any way to prevent the automatic resolution of promise-like objects?
You would wrap it in a container object and then open it with destructuring:
function foo() {
return promiseFoo.then((res) => {
//...
//Construct a `promiseBar` object which is promise-like,
//but also has other uses.
//...
return [promiseBar];
});
}
foo().then(([res]) => {
//Because it is an array, promiseBar is not resolved here
});
To be fair, I've never actually had to do this myself in a few years of using promises, so I'm wondering what you're actually doing - please do share.
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.