简体   繁体   中英

JS: Promise doesn't return value

I need to get a value of an asynchronous function. I tried to use Promise, but that does not work:

const res = new Promise(function (resolve, reject) {
  gm(readStream).size({ bufferStream: true }, function (err, size) {
    if (!err) resolve(size)
  })
})
console.log(res)

The result I get is Promise { <pending> }

Promises are an abstraction for callbacks, not magic. They can't make asynchronous code synchronous.

The correct solution is:

const res = new Promise(function (resolve, reject) {
  gm(readStream).size({ bufferStream: true }, function (err, size) {
    if (err) reject(err);
    else resolve(size);

  })
});

res.then(function(promiseResolutionValue) {
    console.log(res)
})

You could also use async / await here:

const getSize = readStream => {
    return new Promise(function (resolve, reject) {
    gm(readStream).size({ bufferStream: true }, function (err, size) {
      if (err) reject(err);
      else resolve(size);
    })
  });
}


let printSize = async readStream => {
  console.log(`Size is ${await getSize(readStream)}`);
}

Or, if you're using NodeJS (Version 8+), you might be able to adapt your function to use util.promisify .
Other Promise libraries, such as Bluebird , also offer such functions, to easily convert 'standard' node-style functions (functions that have a callback with err, data as arguments) into promise-returning equivalents.

Or just use the callback.

Your code should be looked like this:

const res = new Promise(function (resolve, reject) {
  gm(readStream).size({ bufferStream: true }, function (err, size) {
    if (!err) resolve(size)
    else reject(err)
  })
})

function onResolved(data) {
  console.log('data: ', data);
}

function onRejected(err) {
  console.log('err:', err);
}

res.then(onResolved, onRejected);

Promise does not make your code synchronous, it lets you control when you want to get the result, not immediately like callback style.

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