简体   繁体   中英

How to join promise to a promise chain

I have func1<\/code> function which returns a promise. In func2<\/code> i have started promise chain. What i want to do here is, i want to use func1<\/code> resolve message in old promise chain, and i want this code to be less complex. What is the best way to join func1<\/code> promise to promise chain in func2<\/code>

var func1 = function(){
  return new promise(function(resolve, reject){
    //some operations here
  });
};

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(){
    //this is the old promise chain

        func1()
          .then(function(message,error){
             return message;
             //i want use this return value in old promise chain
          });

         console.log(message);
        //printing  func1 returned message in old promise chain
    })
};

Just return the new promise from within a .then() handler and it will automatically be added to the previous chain and will then control the resolved value of the old promise chain.

The outer promise won't resolve until the newly returned promise is resolved and the inner promise will control the final resolved value. I added the return statement here in front of your call to return func1() to add it to the chain:

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(){
    //this is the old promise chain

        // ADDED return here
        return func1()
          .then(function(message,error){
             return message;
             //i want use this return value in old promise chain
          });
    })
};

There are several other things I'd change in your code because it looks like everything you have above can be distilled down to just this:

var func2 = function () {
    someFuncSync();
    return func1();
};

This allows you do then do:

func2().then(function(message) {
    // process message here
}, function(err) {
    // process err here
});

Summary of changes:

  1. There's no need to wrap someFuncSync() into promise if it's always synchronous. You can just call it and then start your promise chain.
  2. Since standard promises only return a single value (not something like (message, error) , there's really no reason for the callback with return message in it. You can just return the promise directly.
  3. Added return in front of func1() so we are returning the promise.

Man, some of these answers are really overthinking things. The beauty of promises is their simplicity:

return func1().then(func2)

A .then callback may return a promise and it'll be added automatically to the chain:

// createURL() returns a promise that returns a URL.
createURL(...).then(url => {
  return fetch(url)
})
.then(response => {
  // :-)
})

I would do it by adding an extra step to the old promise chain.

This assumes you don't need to use the value resolved from the old promise chain in the new.

var func1 = function(){
  return new promise(function(resolve, reject){
    //some operations here
  });
};

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(arg){

        return promise.all([
            arg, // Argument that original promise resolved with
            func1() // The new extra promise
        ])
    })
    .spread(function(arg, message){
        // Now i have the new message and also the return value from the old promise.
         console.log(message);
        //printing  func1 returned message in old promise chain
    })
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM