How to put a fetch inside a Promise?
up vote
1
down vote
favorite
I'm trying to put a fetch inside a promise, so that I can use it in a Promise.all
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true )
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link))
});
Promise.all( [dbconnect, call] ).then...
Both calls get responses, but it doesn't trigger the Promise.all().then
, what am I doing wrong?
javascript node.js ecmascript-6
add a comment |
up vote
1
down vote
favorite
I'm trying to put a fetch inside a promise, so that I can use it in a Promise.all
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true )
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link))
});
Promise.all( [dbconnect, call] ).then...
Both calls get responses, but it doesn't trigger the Promise.all().then
, what am I doing wrong?
javascript node.js ecmascript-6
fetch
already returns a promise, just use it in Promise.all
– felixmosh
Nov 10 at 18:27
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to put a fetch inside a promise, so that I can use it in a Promise.all
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true )
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link))
});
Promise.all( [dbconnect, call] ).then...
Both calls get responses, but it doesn't trigger the Promise.all().then
, what am I doing wrong?
javascript node.js ecmascript-6
I'm trying to put a fetch inside a promise, so that I can use it in a Promise.all
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true )
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link))
});
Promise.all( [dbconnect, call] ).then...
Both calls get responses, but it doesn't trigger the Promise.all().then
, what am I doing wrong?
javascript node.js ecmascript-6
javascript node.js ecmascript-6
asked Nov 10 at 18:20
Himmators
5,4812684171
5,4812684171
fetch
already returns a promise, just use it in Promise.all
– felixmosh
Nov 10 at 18:27
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29
add a comment |
fetch
already returns a promise, just use it in Promise.all
– felixmosh
Nov 10 at 18:27
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29
fetch
already returns a promise, just use it in Promise.all– felixmosh
Nov 10 at 18:27
fetch
already returns a promise, just use it in Promise.all– felixmosh
Nov 10 at 18:27
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You need to return some result in your promises, otherwise, they won't be passed into the chained .then()
. In your case, if you want to use Promise
constructors, you should explicitly call resolve()
with whatever results you want to pass further, like this:
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true , () =>
resolve()
)
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link).then(resolve))
});
But, as other users already mentioned, you probably don't need to wrap fetch()
and MongoClient.connect()
(since v2.0) into Promise
s as they already return promises. So you can simplify it into:
let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true),
call = fetch(link)
Promise.all( [dbconnect, call] ).then...
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You need to return some result in your promises, otherwise, they won't be passed into the chained .then()
. In your case, if you want to use Promise
constructors, you should explicitly call resolve()
with whatever results you want to pass further, like this:
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true , () =>
resolve()
)
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link).then(resolve))
});
But, as other users already mentioned, you probably don't need to wrap fetch()
and MongoClient.connect()
(since v2.0) into Promise
s as they already return promises. So you can simplify it into:
let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true),
call = fetch(link)
Promise.all( [dbconnect, call] ).then...
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
add a comment |
up vote
2
down vote
accepted
You need to return some result in your promises, otherwise, they won't be passed into the chained .then()
. In your case, if you want to use Promise
constructors, you should explicitly call resolve()
with whatever results you want to pass further, like this:
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true , () =>
resolve()
)
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link).then(resolve))
});
But, as other users already mentioned, you probably don't need to wrap fetch()
and MongoClient.connect()
(since v2.0) into Promise
s as they already return promises. So you can simplify it into:
let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true),
call = fetch(link)
Promise.all( [dbconnect, call] ).then...
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You need to return some result in your promises, otherwise, they won't be passed into the chained .then()
. In your case, if you want to use Promise
constructors, you should explicitly call resolve()
with whatever results you want to pass further, like this:
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true , () =>
resolve()
)
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link).then(resolve))
});
But, as other users already mentioned, you probably don't need to wrap fetch()
and MongoClient.connect()
(since v2.0) into Promise
s as they already return promises. So you can simplify it into:
let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true),
call = fetch(link)
Promise.all( [dbconnect, call] ).then...
You need to return some result in your promises, otherwise, they won't be passed into the chained .then()
. In your case, if you want to use Promise
constructors, you should explicitly call resolve()
with whatever results you want to pass further, like this:
let dbconnect = new Promise((rs, rj)=>
console.log('dbconnect');
require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true , () =>
resolve()
)
),
call = new Promise((rs, rj) =>
console.log('fetch');
fetch(link).then(resolve))
});
But, as other users already mentioned, you probably don't need to wrap fetch()
and MongoClient.connect()
(since v2.0) into Promise
s as they already return promises. So you can simplify it into:
let dbconnect = require('mongodb').MongoClient.connect("mongodb://localhost:27017/mydb", useNewUrlParser: true),
call = fetch(link)
Promise.all( [dbconnect, call] ).then...
edited Nov 10 at 18:38
answered Nov 10 at 18:31
shkaper
48329
48329
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
add a comment |
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
Connect also returns a promise.
– jonrsharpe
Nov 10 at 18:31
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
@jonrsharpe Are you sure? mongodb.github.io/node-mongodb-native/api-generated/… says it returns null and you need to pass a callback
– shkaper
Nov 10 at 18:32
1
1
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
Since 2015, yes: github.com/mongodb/node-mongodb-native/blob/2.2/…
– jonrsharpe
Nov 10 at 18:35
1
1
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
you're right, I'll update the answer
– shkaper
Nov 10 at 18:35
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
Thanks, this was my initial soluton, but it had strange results for me, i'm new to promises pretty much, here is my actual problem: stackoverflow.com/questions/53242204/…
– Himmators
Nov 10 at 18:39
add a comment |
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%2f53242046%2fhow-to-put-a-fetch-inside-a-promise%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
fetch
already returns a promise, just use it in Promise.all– felixmosh
Nov 10 at 18:27
That's what i tried first, just realized my problem was with something else... will close this question!
– Himmators
Nov 10 at 18:29