I want to make the callback wait for the response before executing the next lines of code. I did try to make the callback async
then await
for the response, but that didn't work.
async signup(parent, args, ctx, info) {
const email = args.email;
let res;
verifier.verify(email, function(err, response) {
if (err) console.log(err);
else {
res = response.success;
console.log('before', res); // true or false
}
});
console.log('after', res); // undefined
}
async function signup (parent, args) {
return new Promise(function(resolve, reject) {
verifier.verify(args.email, function(err, response) {
if (err) {
reject(err);
}
else {
resolve(response.success);
}
});
});
}
Create a function and execute it after getting response
async signup(parent, args, ctx, info) {
const email = args.email;
let res;
verifier.verify(email, function(err, response) {
if (err) console.log(err);
else {
res = response.success;
console.log('before', res); // true or false
nextfun(res)
}
});
}
function nextfun(res) {
console.log(res)
}
async signup(parent, args, ctx, info) {
let res = await new Promise(function(resolve, reject) {
verifier.verify(args.email, function(err, response) {
if (err) {
reject(err);
} else {
resolve(response.success);
}
});
});
console.log('after', res); // response.success
}
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.