Este es un fragmento de código que escribí. Tengo problemas para acceder a los datos de Promise para fines posteriores.
function forgotPassword(params) { return new Promise(function (resolve, reject) { return client.methodCall('forgotPassword', params, function (error, value) { if (error === null) { if (value === true) { console.log('Password Sent!!'); //subpy.kill('SIGINT'); return resolve('Password Sent!!'); } else { console.log('User Not Found!!'); //subpy.kill('SIGINT'); return resolve('User Not Found!!'); } } else { console.log('Error while doing the Operation!!'); //subpy.kill('SIGINT'); return reject(error); } }); } ); }Le sugiero que lea los documentos sobre Async Functions and Promises .
En este caso puedes hacer algunas cosas.
new Promise(function (resolve, reject) { return client.methodCall('forgotPassword', params, function (error, value) { if (error === null) { if (value === true) { console.log('Password Sent!!'); //subpy.kill('SIGINT'); return resolve('Password Sent!!'); } else { console.log('User Not Found!!'); //subpy.kill('SIGINT'); return resolve('User Not Found!!'); } } else { console.log('Error while doing the Operation!!'); //subpy.kill('SIGINT'); return reject(error); } }); }) .then(res => console.log(res)) .catch(rej => console.log(rej)); then se llamará si se llama resolve .
Se llamará a la catch si hay un error o se llama a reject .
Otra forma es usar await dentro de una función async para esperar hasta obtener un resultado del objeto de promesa.
function myPromiseFunction(){ return new Promise(function (resolve, reject) {..... } async function myfunction() { try { var res = await myPromiseFunction(); // waits until result from promise console.log(res); } catch (error){ console.log(error); } } myfunction();