How do you properly return multiple values from a promise?

后端 未结 9 1352
慢半拍i
慢半拍i 2020-12-04 14:14

I\'ve recently run into a certain situation a couple of times, which I didn\'t know how to solve properly. Assume the following code:

somethingAsync()
  .the         


        
9条回答
  •  有刺的猬
    2020-12-04 14:44

    Simply make an object and extract arguments from that object.

    let checkIfNumbersAddToTen = function (a, b) {
    return new Promise(function (resolve, reject) {
     let c = parseInt(a)+parseInt(b);
     let promiseResolution = {
         c:c,
         d : c+c,
         x : 'RandomString'
     };
     if(c===10){
         resolve(promiseResolution);
     }else {
         reject('Not 10');
     }
    });
    };
    

    Pull arguments from promiseResolution.

    checkIfNumbersAddToTen(5,5).then(function (arguments) {
    console.log('c:'+arguments.c);
    console.log('d:'+arguments.d);
    console.log('x:'+arguments.x);
    },function (failure) {
    console.log(failure);
    });
    

提交回复
热议问题