简体   繁体   中英

return promises instead of res.json(data) in node.js

I do a post request to payment_url which is triggered the second block of function. But I can't get anything in the then method, because I don't know how to pass the data back to the resolve function.

cilent call

paypal.request.post(payment_url)
  .then(function(data) {
    resolve(data.paymentID);
  })
  .catch(function(err) {
    reject(err);
  });

server code

paypal.payment.create(create_payment_json, function(error, payment) {
  if (error) {
    //throw error, return the promises too
  } else {
    // return promises but how?
   //if I simply do res.json(payment) it's not going to work.
  }
});

I think you have some concepts wrong.

The server response can not be a Promise, since it's already outside the scope of Javascript. In this case we are already speaking about HTTP requests. The type of the response is specified in the header Content-Type , the types available can be checked here .

In your code you are using json.res() , so you will be sending back to the client a text in JSON format.

If you want to get the response from the server through Promises, that must be implemented in the client.

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