My async function enters then before request is completed. Shouldn't Then part of the code executes only after the async function is completed? How to make the function call only when all the function has finished executing?
app.js
var request_test = require('./request_test');
baseUrl = "https://github.com";
promiseFunction().then((result)=>{
console.log("complete")
});
var promiseFunction = async function promiseFunction() {
request_test.request_test(baseUrl);
}
request_test.js
var request = require('request');
var cheerio = require('cheerio');
var request_test = function check(baseUrl) {
console.log("baseUrl:" + baseUrl)
var options = {
url: baseUrl
};
request(options, function (error, response, html) {
if (!error) {
console.log("no error");
}else{
console.log("else js");
console.log(error);
}
});
}
module.exports = {
request_test: request_test
};
In order to use then() you need to return a promise. So here is an example of the good old style promise chain, simply return promise from request_test and once you resolve or reject it, then() will be called:
promiseFunction().then((result) => {
console.log("complete");
});
function promiseFunction() {
return request_test();
}
function request_test() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("no error");
resolve();
}, 1000);
});
}
Or maybe use the modern approach - async method to await call function that returns promise.
promiseFunction();
async function promiseFunction() {
await request_test();
console.log('complete');
}
function request_test() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("no error");
resolve();
}, 1000);
});
}
Your issue is coming from var request_test = function check(baseUrl) { ... inside this function you are not returning promise, you are even returning nothing :)
if you are using async I would go ahead and use the await/async syntax. Also the package request does not return a promise, you have an alternative with request-promise-native. The promise should be the return value of your helper function. It could look like this:
var request_test = require('./request_test');
var baseUrl = "https://github.com";
var promiseFunction = async function () {
var result = await request_test.request_test(baseUrl);
console.log("complete");
}
promiseFunction();
and the module:
var request = require('request-promise-native');
var cheerio = require('cheerio');
var request_test = function check(baseUrl) {
console.log("baseUrl:" + baseUrl)
var options = {
url: baseUrl
};
return request(options).then(function (error, response, html) {
if (!error) {
console.log("no error");
} else{
console.log("else js");
console.log(error);
}
});
}
module.exports = {
request_test: request_test
};
Related
I am working with aws lamda and creating a function returning a promise from it and then consuming it in my client function in node. Due to some reason it is returning Promise and i am not able to figure out what is wrong here. Here is the following code snippet : -
Lamda Function
function paymentConfirmationMessage() {
return new Promise((resolve) => {
setTimeout(() => {
resolve("I came from lamda");
}, 20000);
});
}
exports.handler = async (event) => {
let calculationResult = await paymentConfirmationMessage();
return calculationResult;
}
In node client : -
const https = require("https");
const url = "SomeLamdaurl.com/get";
function getData(url) {
return new Promise(resolve => {
https.get(url, function (error, response, body) {
resolve(body);
});
});
}
async function test(){
var body = await getData(url);
if (body) {
const obj = JSON.parse(body);
console.log('obj', obj);
}
else {
}
}
test();
Additionally when i am making call from api gateway setup in lamda i am getting the desired response.
Any lead will be helpful. Thanks!
Is there any easy way to do recursive call using promise. Here is my sample.
function getData() {
var result=[];
var deferred = Q.defer();
(function fetchData(pageno){
var options = {
method : 'GET',
url : 'example.com/test',
qs:{
pageNo: pageno
}
}
request(options, function (error, response, body) {
if (error)throw new Error(error);
if (body.hasMorePage == true) { //checking is there next page
result.push(body)
fetchData(++body.pageno); // getting next page data
} else {
deferred.resolve(result); // promise resolve when there is no more page
}
});
})(0);
return deferred.promise;
}
getData().then(function(data){
console.log(data)
});
Let's consider API is giving more data in consecutive calls. in order to collect all the data, I need to use some parameter (EX:hasMorePage) from previous call response. I need to go regressive call only for obtaining this scenario, but I would like to know a better(Promise) way.
Most welcome.
async function request(options, callback) {
// simulate server response of example.com/test with 1 second delay
const totalNumberOfPages = 3;
const pageNo = options.qs.pageNo;
await new Promise(resolve => setTimeout(resolve, 1000));
const hasMorePages = pageNo < totalNumberOfPages;
const body = { hasMorePages };
callback(void 0, { body }, body);
}
function getPage(pageNo) {
const options = {
method: 'GET',
url: 'example.com/test',
qs: { pageNo }
};
return new Promise(resolve => request(options, (error, response, body) => {
console.log('response received', response);
if(error) {
throw new Error(error);
}
resolve(body);
}));
}
async function getData() {
const result = [];
for(let i = 1, hasMorePages = true; hasMorePages; i++) {
const body = await getPage(i);
result.push(body);
hasMorePages = body.hasMorePages;
}
return result;
}
getData().then(data => console.log('RESULT', data));
Pretty simple Node.js (console app) code:
var request = require("request");
var q = require("q");
var data = new Object();
var deferred = new q.defer();
var url1 = "https://www.google.com"
var url2 = "https://www.yahoo.com"
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
console.log('begin data1');
requestData(url1) //Promises seem to be working fine here
.then(function () { console.log('finished data1'); }) //At this point, we go back to async, and everything from here down executes at the same time.
.then(function () { console.log('begin data2'); })
.then(function () { requestData(url2); }) //Expected behavior is that it should pause here, and execute everything below after the request is complete.
.then(function () { console.log('finished data2'); })
.then(function () { console.log(data); })
.then(function () { console.log('finished write2'); })
.then(function () { console.log('operation completed!'); });
function requestData(url) {
console.log(url);
data = new Object();
console.log(data);
deferred = new q.defer();
console.log(deferred);
request({
url: url,
json: true
}, function (error, response, obj) {
if (!error && response.statusCode === 200) {
data = obj;
deferred.resolve();
console.log(deferred);
} else {
console.log('err');
}
});
return deferred.promise;
}
The problem is after the first .then statement, everything executes asynchronously. I want the second time I run 'requestData' call to be synchronous like the first.
Pardon my console logs for debugging purposes.
What am I doing wrong here?
The problem is you are not returning a promise in the then methods.
Try this:
...something.then(function() {
return requestData(url2);
}).then(function() { //called after requestData ended});
I have the following two snippets:
1.this is the unit I would like to run:
health.checkHealth = function (req, res) {
async.parallel({
cassandra: function (callback) {
cassandraRepository.ping(callback);
},
kafka: function (callback) {
kafkaHelper.ping(callback);
},
vault: function (callback) {
vaultClient.health()
.then((isOk)=>{
callback(null, isOk)})
.catch(()=>
callback(null, false));
}
}, function (err, result) {
var response = {};
if (result.cassandra) {
response.cassandra = "OK";
} else {
response.cassandra = "Failed";
res.status(500);
}
if (result.kafka) {
response.kafka = "OK";
} else {
response.kafka = "Failed";
res.status(500);
}
if (result.vault) {
response.vault = "OK";
} else {
response.vault = "Failed";
res.status(500);
}
res.send(response);
})
}
2.this is the test to check the unit:
describe('vault is avaliable', function () {
beforeEach(sinon.test(function () {
sinon.stub(vaultClient, "health").resolves(true);
}));
it('should return 200', sinon.test(function (done) {
var response = {
vault: "OK"
};
var req = {};
var res = {};
var spySend = res.send = sinon.spy();
var spyStatus = res.status = sinon.spy();
health.checkHealth(req, res);
expect(spySend.calledOnce).to.equal(true);
expect(spySend.calledWith(response));
expect(spyStatus.calledOnce).to.equal(false);
}));
});
My problem is that when I call checkHealth it proceeds to the next line (expect(spySend.calledOnce).to.equal(true);) without waiting for the vaultClient's promise to complete.
What do I need to do to make the expects run only after the 'checkHealth' was run.
You do not need to use the async - you can use promises directly in your code with Promise.all as others have suggested.
The code here is using promisifyAll from Bluebird, but you can also convert the APIs to use promises yourself.
//.props is bluebird, you can `.all` with native promises too
Promise.props({
cassandra: cassandraRepository.pingAsync(); // created by promisifyAll
kafka: kafkaHelper.pingAsync(),
vault: vaultClient.health()
}).then(results => {
// access results.cassandra, results.kafka and results.vaule here
});
I have an asynchronous nightmare.js process which uses vo.js flow control with a generator:
vo(function *(url) {
return yield request.get(url);
})('http://lapwinglabs.com', function(err, res) {
// ...
})
This needs to return a promise to Hapi (v.13.0.0) with reply() interface. I have seen examples with Bluebird and other promise libraries, eg: How to reply from outside of the hapi.js route handler, but having trouble adapting vo.js. Could someone please provide an example of this?
server.js
server.route({
method: 'GET',
path:'/overview',
handler: function (request, reply) {
let crawl = scrape.doCrawl({"user": USERNAME, "pass": PASSWORD});
reply( ... ).code( 200 );
}
});
scrape.js
module.exports = {
DoCrawl: function(credentials) {
var Nightmare = require('nightmare');
var vo = require('vo');
vo(function *(credentials) {
var nightmare = Nightmare();
var result = yield nightmare
.goto("www.example.com/login")
...
yield nightmare.end();
return result
})(credentials, function(err, res) {
if (err) return console.log(err);
return res
})
}
};
If you wanted to send the result of doCrawl to hapi's reply method, you'll have to convert doCrawl to return a promise. Something like this (untested):
server.js
server.route({
method: 'GET',
path:'/overview',
handler: function (request, reply) {
let crawl = scrape.doCrawl({"user": USERNAME, "pass": PASSWORD});
// crawl is a promise
reply(crawl).code( 200 );
}
});
scrape.js
module.exports = {
doCrawl: function(credentials) {
var Nightmare = require('nightmare');
var vo = require('vo');
return new Promise(function(resolve, reject) {
vo(function *(credentials) {
var nightmare = Nightmare();
var result = yield nightmare
.goto("www.example.com/login")
...
yield nightmare.end();
return result
})(credentials, function(err, res) {
// reject the promise if there is an error
if (err) return reject(err);
// resolve the promise if successful
resolve(res);
})
})
}
};