我试图更好地理解诺言。 我正在对API进行异步调用,然后根据响应,我想做更多的操作,为此,我使用以下2种方法:
方法1:
function getABValues(){
getValueOfA() /*this returns aOutput*/
.then(function (aOutput){
getValueOfB() /*this returns bOutput*/
})
.then(function (bOutput){
console.log(bOutput);
})
}
方法2:
function getABValues(){
return getValueOfA() /*this returns aOutput*/
.then(function (aOutput){
return getValueOfB() /*this returns bOutput*/
})
.then(function (bOutput){
console.log(bOutput);
})
}
以下是函数getValueOfA()和getValueOfB():
getValueOfA()
function getValueOfA(){
return aOutput;
}
getValueOfB():
function getValueOfB(){
return bOutut;
}
如果您注意到getValueOfA()
和getValueOfB()
已经在返回值,那么在使用getValueOfA()
之前,在调用getValueOfB()
和then
时仍需要使用return吗。
方法1不使用return
,而方法2使用return
。
感谢您澄清
答案 0 :(得分:4)
这与诺言没有直接关系。
方法1中的 function getABValues()
没有return语句。它将返回undefined
。
function getABValues()
具有return语句。它将返回评估getValueOfA().then(something).then(something)
的结果,这将是一个希望。
因此,方法2将允许您:
getABValues().then(do_something_with_those_values);
答案 1 :(得分:1)
const API = {
ids: 0,
get: function( value ) {
return new Promise(function( resolve, reject ) {
const timer = Math.random( Math.random() * 2 );
setTimeout(function() {
API.ids += 1;
resolve([{ "id": API.ids, "value": value }]);
}, timer );
});
}
};
// The mock API will give us a promise object.
// We want to return that promise object.
// So we can use the value of the promise in .then()
const getValueOfA = function() {
return API.get( 'value A' );
};
const getValueOfB = function() {
return API.get( 'value B' );
};
// We want to chain both API to resolve in order.
const getABValues = function() {
// If we do not return, we cannot use getABValues().then( ... )
// Outside of this function.
return getValueOfA()
// If we did not return inside getValueOfA(), we would not have the API promise.
// And then this line will throw an error
// Since undefined does not have a method called .then()
.then(function( resolvedValueOfPromiseA ) {
// We can use resolvedValueOfPromiseA here, which will be the value:
// [{"id":1,"value":"value A"}]
console.log( 'inside getValueOfA().then( ... )' );
console.log( JSON.stringify( resolvedValueOfPromiseA ));
// Return getValueOfB() so we can use the resolvedValueOfPromiseB
return getValueOfB();
// We could also use
//
// return getValueOfB( resolvedValueOfPromiseA );
//
// If we change the getValueOfB() function to have a aprameter and use it.
// Then we can use the value of resolvedValueOfPromiseA
// Inside getValueOfB()
})
.then(function( resolvedValueOfPromiseB ) {
console.log( 'inside getValueOfA().then( ... ).then( ... )' );
console.log( JSON.stringify( resolvedValueOfPromiseB ));
return resolvedValueOfPromiseB;
});
};
// Use the function on the outside.
// We can call .then() since getABValues() returns the entire promise chain.
getABValues()
.then(function( resolvedValueOfPromiseBReturnedByThen ) {
console.log( 'inside getABValues().then( ... )' );
console.log( JSON.stringify( resolvedValueOfPromiseBReturnedByThen ));
});
// Notice how here we only have the result of getValueOfB()
// Since we did not do anything with resolvedValueOfPromiseA in the .then() function
// THat ahd access to resolvedValueOfPromiseA