Want to get the response of nested api call and then send back to my middle ware node js api
up vote
-1
down vote
favorite
I want to use async await in the nested api call but not figure out how ? So the response of 2nd api send back to apisign_in response which i get from front end is there any solution here is my code:
....
app.post("/api/sign_in", (req, res) => {
console.log("i am in login call");
const resp = scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into /api/sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
return apiresponse;
}
console.log("==============");
console.log(response.body);
return response.body;
}
);
}
....
javascript node.js api post async-await
add a comment |
up vote
-1
down vote
favorite
I want to use async await in the nested api call but not figure out how ? So the response of 2nd api send back to apisign_in response which i get from front end is there any solution here is my code:
....
app.post("/api/sign_in", (req, res) => {
console.log("i am in login call");
const resp = scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into /api/sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
return apiresponse;
}
console.log("==============");
console.log(response.body);
return response.body;
}
);
}
....
javascript node.js api post async-await
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument toscaryClown()
– charlietfl
Nov 22 at 5:11
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I want to use async await in the nested api call but not figure out how ? So the response of 2nd api send back to apisign_in response which i get from front end is there any solution here is my code:
....
app.post("/api/sign_in", (req, res) => {
console.log("i am in login call");
const resp = scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into /api/sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
return apiresponse;
}
console.log("==============");
console.log(response.body);
return response.body;
}
);
}
....
javascript node.js api post async-await
I want to use async await in the nested api call but not figure out how ? So the response of 2nd api send back to apisign_in response which i get from front end is there any solution here is my code:
....
app.post("/api/sign_in", (req, res) => {
console.log("i am in login call");
const resp = scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into /api/sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
return apiresponse;
}
console.log("==============");
console.log(response.body);
return response.body;
}
);
}
....
javascript node.js api post async-await
javascript node.js api post async-await
asked Nov 22 at 5:02
Hamza Mazhar
12
12
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument toscaryClown()
– charlietfl
Nov 22 at 5:11
add a comment |
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument toscaryClown()
– charlietfl
Nov 22 at 5:11
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument to
scaryClown()– charlietfl
Nov 22 at 5:11
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument to
scaryClown()– charlietfl
Nov 22 at 5:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
To wait for the response of your scaryClown function you can use await before
your function call.
app.post("/api/sign_in", async (req, res) => {
console.log("i am in login call");
const resp = await scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into / api / sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
return new Promise((resolve, reject) => {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
resolve(apiresponse);
}
console.log("==============");
console.log(response.body);
resolve(response.body);
}
);
});
}
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424180%2fwant-to-get-the-response-of-nested-api-call-and-then-send-back-to-my-middle-ware%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
To wait for the response of your scaryClown function you can use await before
your function call.
app.post("/api/sign_in", async (req, res) => {
console.log("i am in login call");
const resp = await scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into / api / sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
return new Promise((resolve, reject) => {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
resolve(apiresponse);
}
console.log("==============");
console.log(response.body);
resolve(response.body);
}
);
});
}
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
add a comment |
up vote
0
down vote
accepted
To wait for the response of your scaryClown function you can use await before
your function call.
app.post("/api/sign_in", async (req, res) => {
console.log("i am in login call");
const resp = await scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into / api / sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
return new Promise((resolve, reject) => {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
resolve(apiresponse);
}
console.log("==============");
console.log(response.body);
resolve(response.body);
}
);
});
}
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
To wait for the response of your scaryClown function you can use await before
your function call.
app.post("/api/sign_in", async (req, res) => {
console.log("i am in login call");
const resp = await scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into / api / sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
return new Promise((resolve, reject) => {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
resolve(apiresponse);
}
console.log("==============");
console.log(response.body);
resolve(response.body);
}
);
});
}
To wait for the response of your scaryClown function you can use await before
your function call.
app.post("/api/sign_in", async (req, res) => {
console.log("i am in login call");
const resp = await scaryClown(req.body.email, req.body.password);
console.log("here you should wait for above response and then pass
this response into / api / sign_in response");
res.send(resp);
});
function scaryClown(email, password) {
return new Promise((resolve, reject) => {
request.post(
key.sign_in_url,
{
json: {
email: email,
password: password
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
email = response.headers.uid;
key.email = response.headers.uid;
key.client = response.headers.client;
var result = ;
for (var i in response.headers) result.push(i, response.headers[i]);
access_token = result[17];
key.access_token = access_token;
key.expiry = response.headers.expiry;
apiresponse = response.body;
console.log("apiresponse");
console.log(apiresponse);
resolve(apiresponse);
}
console.log("==============");
console.log(response.body);
resolve(response.body);
}
);
});
}
edited Nov 22 at 12:06
answered Nov 22 at 5:28
Yogesh.Kathayat
403110
403110
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
add a comment |
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
i already try this approach but this not work as in node i get the empty response i want to get the response of my scary clown then this response pass to the sign_in api
– Hamza Mazhar
Nov 22 at 10:28
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
ok in that case you can return a promise from your scaryClown function. it should work. i have edited the code in my response.
– Yogesh.Kathayat
Nov 22 at 12:06
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424180%2fwant-to-get-the-response-of-nested-api-call-and-then-send-back-to-my-middle-ware%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
To use async/await you need version of request library that returns promises. There are suggestions in the request library docs. Otherwise add a callback argument to
scaryClown()– charlietfl
Nov 22 at 5:11