在JavaScript承诺中使用return

问题描述 投票:0回答:2

我想更好地理解承诺。我正在对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

谢谢你的澄清

javascript jquery asynchronous promise callback
2个回答
4
投票

这与承诺没有直接关系。

方法1中的function getABValues()没有退货声明。它将返回undefined

方法2中的function getABValues()有一个退货声明。它将返回评估getValueOfA().then(something).then(something)的结果,这将是一个承诺。

方法2将允许您:

getABValues().then(do_something_with_those_values);

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
© www.soinside.com 2019 - 2024. All rights reserved.