0

I'm new to node.js and I'm trying to migrate the backend of my company from python to node.js

Right now I created a node.js server using express. The route works fine. My company uses WooCommerce, and they have a node.js library. My code is this one:

const WooCommerceAPI = require("woocommerce-api");

class WooCommerceController {
  async getOrders(req, res) {
    const orders = await WooCommerce.get("orders", function(err, data, res) {
      return res;
    });
    return res.json(orders);
  }
}

module.exports = new WooCommerceController();

I know that

WooCommerce.get("orders", function(err, data, res) {
      console.log(res);
    });

works, because if I execute this function it returns the list of orders of my company, however if I try to put it inside this async await, it returns the status of the WooCommerce API, not the response of the API.

What am I doing wrong?

3
  • Can you share your code with async await, that is not working? Commented Apr 15, 2019 at 2:28
  • WooCommerce.get() would need to return a promise to use await. It is using a callback not promise Commented Apr 15, 2019 at 2:28
  • I believe WooCommerce.getAsync serves that purpose, let me check Commented Apr 15, 2019 at 2:33

2 Answers 2

1

WooCommerce.get works with a callback, while await only works with functions that return a Promise. So either you need to create a promise yourself, and manually resolve it in the callback, or you can use util.promisify to automatically convert the callback-function (with an (err, value) callback parameters) to a Promise-style function.

const wooGetPromise = util.promisify(WooCommerce.get);
...
const orders = await wooGetPromise("orders");
...

EDIT: Thanks to RichS for looking up the API: my wooGetPromise already exists as WooCommerce.getAsync.

The options for using that function are:

  1. With then:
    WooCommerce.getAsync('orders').then(function(result) {
       return JSON.parse(result.toJSON().body);
    });
  1. With await (only in async function):
    var result = await WooCommerce.getAsync('orders');
    var json = JSON.parse(result.toJSON().body);
Sign up to request clarification or add additional context in comments.

7 Comments

await only works with functions that return a Promise?
See also: npmjs.com/package/woocommerce-api#promified-methods (the npm repo mispelled "promisifed"). I think the WooCommerce.getAsync() method could be used... (Yeah, the library uses bluebird under the hood...)
@Ele Good point. await only usefully works with functions that return a Promise.
Thank you guys! This promified methods weren't in their documentation. I had a look at the npm repo and it worked! Thank you so much for the ultra fast solution!
@Ele: That too, but that's not what I'm saying. You are talking about the function that surrounds await (which is off-topic here). I'm talking about the value that follows the await keyword (which is relevant here, because WooCommerce.get(...) doesn't return a promise, and is thus useless waste of cycles to await it).
|
0

hello mate await usually resolves a promise so the function that is being called after the await key word should be a function that returns a promise or an asyn function which by default returns a promise.

Async function

WooCommerce.get("orders", async function(err, data, res) {
      return res;
}); 

Or a function that returns a promise

WooCommerce.get("orders", function(err, data, res) {
  return new Promise(function(resolve, reject){
   err ? reject(err) : resolve(res);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.