Node.js - module: nock - .reply with a callback doesn't work

I'm using nock module to intercept HTTP calls to my API service (still undeveloped) and return some mock data that I have in a temporary database.

I notice the http requests are intercepted properly, but the problem that I see is that before my function gets required data from temp DB, the nock replies.

For a simple demonstration, look at the code below:

var nock = require('nock');

var nockReq = nock("http://localhost:8000")

    .post("/sample-endpoint")

    .reply(200, function (uri, requestBody) {
        setTimeout(function() {
            return {"result": "TIMED OUT"}
        }, 2000
    );

With the above code, when I don't use a time-out, I get the data returned properly as expected. But with this time-out, the nock doesn't seem to wait for the callback, instead proceed responding an empty 200 response.

I'm looking for some urgent help to overcome this problem.

You just put the function in setTimeOut to the next tick with nothing returned. Try add return before setTimeOut.