使用 Promise 是否有一种机制可以访问 .then() 链中的最终 return 值?
With a Promise is there a mechanism to access the final return value in a .then() chain?
问题: Promise
是否有一种机制可以访问 .then()
链中的最终 return 值?
背景 我需要在一个对象上运行一系列checks/adjustments。一些检查是异步的,例如将数据与 mongodb 中存储的数据进行比较。因为有些检查是异步的,所以我相信 Promise
链可能是正确的选择。但是,由于必须按特定顺序进行检查,因此 Promise.all()
将不起作用。如果在完成所有检查和调整后可能出现 Promise
链,我不确定如何从链中的最后一个 .then()
检索对象。也许我正在使用错误的工具来解决这个问题。
下面的代码是一个简化的例子。 myObject 通过 .then()
语句链,但我不确定如何检索最终的、更新的对象,或者这是否可能。
function promisesPromises() {
return new Promise( function(resolve, reject) {
let x = {
name: 'super duper',
randomDataOne: 10000,
randomDataTwo: 5000
};
if (x) {
resolve(x);
} else {
reject('uh oh');
}
});
}
function firstAdjustment(myObject) {
myObject.randomDataOne += 1000;
return myObject
}
function secondAdjustment(myObject) {
myObject.randomDataTwo += 500;
return myObject;
}
promisesPromises()
.then(firstAdjustment)
.then(secondAdjustment);
我会 async/await
尝试简化事情。
注意:异步函数总是return一个承诺。因此,为了获取数据,您需要 await
用于另一个异步函数中的 returned 数据,或者您需要使用 then()
。我在下面的代码中给出了两个示例。
function promisesPromises() {
return new Promise( function(resolve, reject) {
let x = {
name: 'super duper',
randomDataOne: 10000,
randomDataTwo: 5000
};
// Promise set to resolve after 5 seconds
if (x) {
setTimeout(() => {
resolve(x);
}, 5000)
} else {
reject('uh oh');
}
});
}
function firstAdjustment(myObject) {
myObject.randomDataOne += 1000;
return myObject
}
function secondAdjustment(myObject) {
myObject.randomDataTwo += 500;
return myObject;
}
const asyncFunction = async () => {
const myObject = await promisesPromises()
firstAdjustment(myObject)
secondAdjustment(myObject)
return myObject
}
// Example retrieving data with then()
asyncFunction().then(res => console.log("Example using then(): ", res))
// Example retrieving data inside async function
const anotherAsyncFunction = async () => {
const result = await asyncFunction()
console.log("Example using async(): ", result)
}
anotherAsyncFunction()
// Timer countdown to promise resolve
let count = 1
const timer = setInterval(() => {
console.log(count++)
if (count > 4) clearInterval(timer)
}, 1000)
问题: Promise
是否有一种机制可以访问 .then()
链中的最终 return 值?
背景 我需要在一个对象上运行一系列checks/adjustments。一些检查是异步的,例如将数据与 mongodb 中存储的数据进行比较。因为有些检查是异步的,所以我相信 Promise
链可能是正确的选择。但是,由于必须按特定顺序进行检查,因此 Promise.all()
将不起作用。如果在完成所有检查和调整后可能出现 Promise
链,我不确定如何从链中的最后一个 .then()
检索对象。也许我正在使用错误的工具来解决这个问题。
下面的代码是一个简化的例子。 myObject 通过 .then()
语句链,但我不确定如何检索最终的、更新的对象,或者这是否可能。
function promisesPromises() {
return new Promise( function(resolve, reject) {
let x = {
name: 'super duper',
randomDataOne: 10000,
randomDataTwo: 5000
};
if (x) {
resolve(x);
} else {
reject('uh oh');
}
});
}
function firstAdjustment(myObject) {
myObject.randomDataOne += 1000;
return myObject
}
function secondAdjustment(myObject) {
myObject.randomDataTwo += 500;
return myObject;
}
promisesPromises()
.then(firstAdjustment)
.then(secondAdjustment);
我会 async/await
尝试简化事情。
注意:异步函数总是return一个承诺。因此,为了获取数据,您需要 await
用于另一个异步函数中的 returned 数据,或者您需要使用 then()
。我在下面的代码中给出了两个示例。
function promisesPromises() {
return new Promise( function(resolve, reject) {
let x = {
name: 'super duper',
randomDataOne: 10000,
randomDataTwo: 5000
};
// Promise set to resolve after 5 seconds
if (x) {
setTimeout(() => {
resolve(x);
}, 5000)
} else {
reject('uh oh');
}
});
}
function firstAdjustment(myObject) {
myObject.randomDataOne += 1000;
return myObject
}
function secondAdjustment(myObject) {
myObject.randomDataTwo += 500;
return myObject;
}
const asyncFunction = async () => {
const myObject = await promisesPromises()
firstAdjustment(myObject)
secondAdjustment(myObject)
return myObject
}
// Example retrieving data with then()
asyncFunction().then(res => console.log("Example using then(): ", res))
// Example retrieving data inside async function
const anotherAsyncFunction = async () => {
const result = await asyncFunction()
console.log("Example using async(): ", result)
}
anotherAsyncFunction()
// Timer countdown to promise resolve
let count = 1
const timer = setInterval(() => {
console.log(count++)
if (count > 4) clearInterval(timer)
}, 1000)