[英]Returning a promise within a promise
What's the most kosher way of getting the desired result WITHOUT nesting both promised within a new promise and calling resolve/reject? 在不嵌套既在新promise中调用promise又调用resolve / reject的情况下获得期望结果的最简洁的方法是什么?
Example 例
const transferFile = (fileName, bucketName)=>{
const destinationBucket = gcs.bucket(bucketName);
return exists = destinationBucket.file(fileName).exists()
.then(exists=>{
if(exists[0]){
return true;
} else {
// calling another method that returns a promise
return file.move(destinationBucket)
.then(yay=>{
// return yay
return yay;
});
}
});
}
transferFile(image, 'bucketName')
.then(exists=>{
console.log(exists);
transferFile(video, 'bucketName');
})
// getting undefined rather than yay
.then(yay)...
I wish to return the yay
value from file.move
to the base promise chain. 我希望将yay
值从file.move
返回到基本promise链。 Currently no thrown errors from file.move
will be caught in the base promise chain, nor is the value of yay
being passed. 当前, file.move
不会引发任何错误,也不会在基本file.move
链中捕获该错误,也不会传递yay
的值。
transferFile
returns the promise, but you discard it in the second call. transferFile
返回承诺,但是您在第二次调用中将其丢弃。
So notice that I return it within the then
function. 因此请注意,我在then
函数中将then
返回。
transferFile(image, 'bucketName')
.then(exists=>{
console.log(exists);
return transferFile(video, 'bucketName');
})
// getting undefined rather than yay
.then(yay)...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.