简体   繁体   中英

Utilize console logging in native javascript promise chain

I have a native javascript promise chain that looks a little like this:

function chain() {
  promiseFunction1().then(function(data) {

    console.log("some message");
    return promiseFunction2();

  }).then(function(data) {

    console.log("some message");
    return promiseFunction3();

  }).then(function(data) {

    console.log("some message");
    return promiseFunction4();

  }).catch(function(error) {
    console.error(error.stack);
  });

}

the promise functions would look a little like this:

function promiseFunction() {
  return new Promise(function(resolve,reject) {
     someCallbackfunction(arg, function(err, data){
        if (err) {
          return reject(err);
        }
        console.log("some message");
        return resolve(data);
     });
  });
}

My code seems to resolve fine from what I can tell (no errors and I can tell from terminal feedback that the operations I needed started running) but no matter what I seem to try I cannot for the life of me seem to get any form of console logging.

1) Why are these statements not printing like I expect them to?

2) How can I go about getting back my verbose output?

It turned out that in the end the problem was external. Somewhere in my promise chain I called an external library with a callback function but the library never responded causing my chain to wait forever.

If you experience something similar I encourage you to double check all of the functions in the chain for a similar occurrence.

A quick way to debug this could be to place a timeout in each promise function in the chain that resolves it after x amount of time, that way you can at least get your logging results without having to stumble in the dark forever.

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