简体   繁体   English

在Nodejs中,如何等待过程直到从函数返回响应

[英]In Nodejs, how wait process until response back from function

I am new to Nodejs. 我是Nodejs的新手。 Process is not waiting until response back from function. 流程不等到功能返回响应。 Because of asynchronous calling of Nodejs. 由于Nodejs的异步调用。 how to make synchronous. 如何使同步。 Please help me. 请帮我。

Here is my code 这是我的代码

module.exports.signup = function(req, res){
console.log('signup');
User.findOne({'emails.email' : req.body.email}, function(err, doc) {
if (doc === null) {
var vr_token= genRandomString(16);
var ex_date = Date.now();
  var user = new User();
      user.emails.push({
        email : req.body.email,
        email_verification_token : vr_token,
        verify_key_expire : ex_date });
        user.save(function(err2,user1) {
          if (!err2) {
          var result = send_email.sync(vr_token);//process not waiting
            if(result) {
              res.json ({
                status: 200,
                message:"mail sent successfully",
                data:user1
              })
            }
          } 
       });
     }
  })      
}

here is my function 这是我的功能

function send_email(vr_token){
var mailOpts = {
from: process.env.Mail_From_Addr,
to: 'xxxxxxxxxxxxx',
subject: 'Verify Your New Account Email',
html:'<p>Hello,</p>code : '+vr_token
 };
   mailgun.messages().send(mailOpts, function (err, response){
    if (!err){
    return true;
     }else{
     return false;
     }
  })
}

Add a callback to it: 向其添加回调:

function send_email(vr_token, callback){
  var mailOpts = {
  from: process.env.Mail_From_Addr,
  to: 'xxxxxxxxxxxxx',
  subject: 'Verify Your New Account Email',
  html:'<p>Hello,</p>code : '+vr_token
  };
  mailgun.messages().send(mailOpts, function (err, response){
    callback(null, !err);
  })
}

The Code: 编码:

module.exports.signup = function(req, res){
console.log('signup');
User.findOne({'emails.email' : req.body.email}, function(err, doc) {
if (doc === null) {
var vr_token= genRandomString(16);
var ex_date = Date.now();
  var user = new User();
      user.emails.push({
        email : req.body.email,
        email_verification_token : vr_token,
        verify_key_expire : ex_date });
        user.save(function(err2,user1) {
          if (!err2) {
            send_email.sync(vr_token, function(err, result){
              if(result) {
                res.json ({
                  status: 200,
                  message:"mail sent successfully",
                  data:user1
                })
              }
            });

          } 
      });
    }
  })      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使服务器中的快速路由等到它收到来自另一个nodejs进程的套接字io消息,然后再向客户端返回响应? - how to make express route in server wait until it receives a socket io message from another nodejs process before returning response to client? 如何在NodeJs中发送回响应之前等待循环完成? - How to wait for a loop to finish running before sending back a response in NodeJs? 如何从 napi 本机代码调用 nodejs 异步函数并等待异步承诺解决 - How to call a nodejs async function from napi native code and wait until the async promise is resolved 如何等到节点的回调 function 执行 - How to wait until the node's call back function executed NodeJs 函数等待直到回调返回值 - NodeJs function wait until callback return value 如何从Node.js中的子进程捕获响应 - How to capture response from child process in nodejs Node.js-SolrClient,如何等待响应 - Nodejs - SolrClient, how to wait for response 如何在 nodejs 上等待 msSQLdb 响应 - How to wait for msSQLdb response on nodejs 如何可编程地等待进程启动端点 - How programmably wait until the process will start the endpoint 如何强制此 NodeJS 函数等到 fs.writeFile 完成? - How can I force this NodeJS function to wait until fs.writeFile has completed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM