Async IIFE module - cache result and do not return promises in further requiresIs Safari on iOS 6 caching $.ajax results?How do I access previous promise results in a .then() chain?How can I use async/await at the top level?Shouldn't async return a Promise without explicitly wrapping the return value in a Promise?Module exports promise result on fulfilledHow to make HTTP request async/await in Cloud Functions for Firebase?Javascript promise not being returned with async/awaitasync function equivalent of a PromisePromises - How to make asynchronous code execute synchronous without async / await?async await setstate and returning promises
Twin primes whose sum is a cube
Can a rocket refuel on Mars from water?
Emailing HOD to enhance faculty application
I'm flying to France today and my passport expires in less than 2 months
Famous Pre Reformation Christian Pastors (Non Catholic and Non Orthodox)
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
Where does SFDX store details about scratch orgs?
Why is Collection not simply treated as Collection<?>
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
What exploit are these user agents trying to use?
What to put in ESTA if staying in US for a few days before going on to Canada
Will google still index a page if I use a $_SESSION variable?
Do I have a twin with permutated remainders?
Alternative to sending password over mail?
Arrow those variables!
Forgetting the musical notes while performing in concert
Did converts (ger tzedek) in ancient Israel own land?
In a Spin are Both Wings Stalled?
I Accidentally Deleted a Stock Terminal Theme
Why doesn't H₄O²⁺ exist?
Is it canonical bit space?
What's the point of deactivating Num Lock on login screens?
AES: Why is it a good practice to use only the first 16bytes of a hash for encryption?
Is it legal for company to use my work email to pretend I still work there?
Async IIFE module - cache result and do not return promises in further requires
Is Safari on iOS 6 caching $.ajax results?How do I access previous promise results in a .then() chain?How can I use async/await at the top level?Shouldn't async return a Promise without explicitly wrapping the return value in a Promise?Module exports promise result on fulfilledHow to make HTTP request async/await in Cloud Functions for Firebase?Javascript promise not being returned with async/awaitasync function equivalent of a PromisePromises - How to make asynchronous code execute synchronous without async / await?async await setstate and returning promises
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Consider having following files:
index.js
(async () =>
const iife = await require('./asyncIIFE');
console.log('[index.js]', iife);
require('./module');
)();
asyncIIFE.js
module.exports = (async () => 'Testing async IIFE')();
module.js
const iife = require('./asyncIIFE');
(() =>
// Should print Testing async IIFE
console.log('[module.js]', iife); // Promise
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
The main idea is to calculate all the values in entry point (index.js) and then use them without recalculating in other files. The first require('asyncIIFE') should be asynchronous, next should immediately return value.
It should look like:
Require all async modules in index.js and await for results
All further requires of the same async modules in other files (eg.
module.js) should immediately return value without need for await/.then()
Code output:
[index.js] Testing async IIFE
[module.js] Promise 'Testing async IIFE'
[module.js] Testing async IIFE
In short, I want to get rid of that promise in module.js file and get cached value. How can I achieve that?
javascript node.js async-await es6-promise es6-modules
|
show 1 more comment
Consider having following files:
index.js
(async () =>
const iife = await require('./asyncIIFE');
console.log('[index.js]', iife);
require('./module');
)();
asyncIIFE.js
module.exports = (async () => 'Testing async IIFE')();
module.js
const iife = require('./asyncIIFE');
(() =>
// Should print Testing async IIFE
console.log('[module.js]', iife); // Promise
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
The main idea is to calculate all the values in entry point (index.js) and then use them without recalculating in other files. The first require('asyncIIFE') should be asynchronous, next should immediately return value.
It should look like:
Require all async modules in index.js and await for results
All further requires of the same async modules in other files (eg.
module.js) should immediately return value without need for await/.then()
Code output:
[index.js] Testing async IIFE
[module.js] Promise 'Testing async IIFE'
[module.js] Testing async IIFE
In short, I want to get rid of that promise in module.js file and get cached value. How can I achieve that?
javascript node.js async-await es6-promise es6-modules
1
I would send to the the non async modules the values calculated as param.require('./module')(iife);and create a closure to inject the param in your module.
– Dez
Mar 8 at 23:34
2
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
1
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
1
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46
|
show 1 more comment
Consider having following files:
index.js
(async () =>
const iife = await require('./asyncIIFE');
console.log('[index.js]', iife);
require('./module');
)();
asyncIIFE.js
module.exports = (async () => 'Testing async IIFE')();
module.js
const iife = require('./asyncIIFE');
(() =>
// Should print Testing async IIFE
console.log('[module.js]', iife); // Promise
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
The main idea is to calculate all the values in entry point (index.js) and then use them without recalculating in other files. The first require('asyncIIFE') should be asynchronous, next should immediately return value.
It should look like:
Require all async modules in index.js and await for results
All further requires of the same async modules in other files (eg.
module.js) should immediately return value without need for await/.then()
Code output:
[index.js] Testing async IIFE
[module.js] Promise 'Testing async IIFE'
[module.js] Testing async IIFE
In short, I want to get rid of that promise in module.js file and get cached value. How can I achieve that?
javascript node.js async-await es6-promise es6-modules
Consider having following files:
index.js
(async () =>
const iife = await require('./asyncIIFE');
console.log('[index.js]', iife);
require('./module');
)();
asyncIIFE.js
module.exports = (async () => 'Testing async IIFE')();
module.js
const iife = require('./asyncIIFE');
(() =>
// Should print Testing async IIFE
console.log('[module.js]', iife); // Promise
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
The main idea is to calculate all the values in entry point (index.js) and then use them without recalculating in other files. The first require('asyncIIFE') should be asynchronous, next should immediately return value.
It should look like:
Require all async modules in index.js and await for results
All further requires of the same async modules in other files (eg.
module.js) should immediately return value without need for await/.then()
Code output:
[index.js] Testing async IIFE
[module.js] Promise 'Testing async IIFE'
[module.js] Testing async IIFE
In short, I want to get rid of that promise in module.js file and get cached value. How can I achieve that?
javascript node.js async-await es6-promise es6-modules
javascript node.js async-await es6-promise es6-modules
asked Mar 8 at 23:23
Sebastian SzczepańskiSebastian Szczepański
196
196
1
I would send to the the non async modules the values calculated as param.require('./module')(iife);and create a closure to inject the param in your module.
– Dez
Mar 8 at 23:34
2
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
1
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
1
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46
|
show 1 more comment
1
I would send to the the non async modules the values calculated as param.require('./module')(iife);and create a closure to inject the param in your module.
– Dez
Mar 8 at 23:34
2
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
1
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
1
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46
1
1
I would send to the the non async modules the values calculated as param.
require('./module')(iife); and create a closure to inject the param in your module.– Dez
Mar 8 at 23:34
I would send to the the non async modules the values calculated as param.
require('./module')(iife); and create a closure to inject the param in your module.– Dez
Mar 8 at 23:34
2
2
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
1
1
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
1
1
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46
|
show 1 more comment
1 Answer
1
active
oldest
votes
While you are importing async.js in your module.js file, you aren't resolving the function.
In module.js
const iife = require('./async');
(async () =>
// Should print Testing async IIFE
console.log('[module.js]', await iife); // Promise
// Notice how I added the await keyword, that's there to resolve the promise
// Function has also changed to asynchronous to use await.
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
Basically, you aren't resolving the file you imported, and since the file returns a promise, your console logs a promise.
index.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
async.js
module.exports = (async () => 'Testing async IIFE')();
module.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
Btw, I changed the filenames a bit. Hope that doesn't affect anything.
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
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',
autoActivateHeartbeat: false,
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%2f55072361%2fasync-iife-module-cache-result-and-do-not-return-promises-in-further-requires%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
While you are importing async.js in your module.js file, you aren't resolving the function.
In module.js
const iife = require('./async');
(async () =>
// Should print Testing async IIFE
console.log('[module.js]', await iife); // Promise
// Notice how I added the await keyword, that's there to resolve the promise
// Function has also changed to asynchronous to use await.
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
Basically, you aren't resolving the file you imported, and since the file returns a promise, your console logs a promise.
index.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
async.js
module.exports = (async () => 'Testing async IIFE')();
module.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
Btw, I changed the filenames a bit. Hope that doesn't affect anything.
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
add a comment |
While you are importing async.js in your module.js file, you aren't resolving the function.
In module.js
const iife = require('./async');
(async () =>
// Should print Testing async IIFE
console.log('[module.js]', await iife); // Promise
// Notice how I added the await keyword, that's there to resolve the promise
// Function has also changed to asynchronous to use await.
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
Basically, you aren't resolving the file you imported, and since the file returns a promise, your console logs a promise.
index.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
async.js
module.exports = (async () => 'Testing async IIFE')();
module.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
Btw, I changed the filenames a bit. Hope that doesn't affect anything.
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
add a comment |
While you are importing async.js in your module.js file, you aren't resolving the function.
In module.js
const iife = require('./async');
(async () =>
// Should print Testing async IIFE
console.log('[module.js]', await iife); // Promise
// Notice how I added the await keyword, that's there to resolve the promise
// Function has also changed to asynchronous to use await.
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
Basically, you aren't resolving the file you imported, and since the file returns a promise, your console logs a promise.
index.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
async.js
module.exports = (async () => 'Testing async IIFE')();
module.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
Btw, I changed the filenames a bit. Hope that doesn't affect anything.
While you are importing async.js in your module.js file, you aren't resolving the function.
In module.js
const iife = require('./async');
(async () =>
// Should print Testing async IIFE
console.log('[module.js]', await iife); // Promise
// Notice how I added the await keyword, that's there to resolve the promise
// Function has also changed to asynchronous to use await.
iife.then(text => console.log('[module.js]', text)); // Testing async IIFE
)();
Basically, you aren't resolving the file you imported, and since the file returns a promise, your console logs a promise.
index.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
async.js
module.exports = (async () => 'Testing async IIFE')();
module.js
(async () =>
const iife = await require('./async');
console.log('[index.js]', iife);
require('./module');
)();
Btw, I changed the filenames a bit. Hope that doesn't affect anything.
answered Mar 8 at 23:43
8333883338
1
1
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
add a comment |
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
I explicitly said that I do not want to use await but thanks for your answer.
– Sebastian Szczepański
Mar 9 at 7:35
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.
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%2f55072361%2fasync-iife-module-cache-result-and-do-not-return-promises-in-further-requires%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
1
I would send to the the non async modules the values calculated as param.
require('./module')(iife);and create a closure to inject the param in your module.– Dez
Mar 8 at 23:34
2
Ew. A function should never return one type of thing the first time it is called, then something else entirely the second. Instead, have it always return the promise. Simply don't recreate the promise after it has been created.
– Kevin B
Mar 8 at 23:34
Yes, that would definitely help. However, I was wondering if there is an other way to do that. I may have nested modules requires in the future and it would be a pain to pass the object through all of these. I could also require them in async closure but i don’t think it’s a good idea (requires should be top level?)
– Sebastian Szczepański
Mar 8 at 23:39
1
I agree with @KevinB, functions should always return the same type of result. however you can cache the result in the promise using a once function, and immediately return a resolved promise on subsequent calls.
– Philippe
Mar 8 at 23:44
1
here is an example of a once function. davidwalsh.name/javascript-once
– Philippe
Mar 8 at 23:46