Created
December 17, 2019 06:37
-
-
Save mahidaparth7/491b805579c694f409d5a0787bbdfa81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var request = require("request"); | |
| let asyncRequest; | |
| let reqPromise = new Promise(function(resolve, reject) { | |
| //first query | |
| request("http://www.google.com", function(error, response, body) { | |
| console.log("called 1"); | |
| if (error) { | |
| reject("hi, this is error"); | |
| } else { | |
| //second query that is dependent on first query | |
| request("http://www.stackoverflow.com", function(error, response, body) { | |
| if (error) { | |
| reject("hi, this is error"); | |
| } else { | |
| resolve({ name: "stack", code: response.statusCode }); | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| let reqPromiseTwo = new Promise(function(resolve, reject) { | |
| //first query | |
| request("http://www.google.com", function(error, response, body) { | |
| console.log("called 2"); | |
| if (error) { | |
| reject("hi, this is error"); | |
| } else { | |
| //second query that is dependent on first query | |
| request("http://www.stackoverflow.com", function(error, response, body) { | |
| if (error) { | |
| reject("hi, this is error"); | |
| } else { | |
| resolve({ name: "stack", code: response.statusCode }); | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| (async function() { | |
| try { | |
| console.time("response in"); | |
| let response = await reqPromise; | |
| console.log("in between"); | |
| let response2 = await reqPromiseTwo; | |
| console.log(response, response2); | |
| console.timeEnd("response in"); | |
| } catch (error) { | |
| //do nothing if error throws | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment