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?










share|improve this question





















  • 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














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?










share|improve this question





















  • 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












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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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 Promises 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...





share|improve this answer






















  • 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










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
);



);













 

draft saved


draft discarded


















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

























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 Promises 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...





share|improve this answer






















  • 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














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 Promises 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...





share|improve this answer






















  • 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












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 Promises 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...





share|improve this answer














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 Promises 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...






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線