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;








-1















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:



  1. Require all async modules in index.js and await for results


  2. 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?










share|improve this question

















  • 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















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:



  1. Require all async modules in index.js and await for results


  2. 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?










share|improve this question

















  • 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








-1








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:



  1. Require all async modules in index.js and await for results


  2. 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?










share|improve this question














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:



  1. Require all async modules in index.js and await for results


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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























  • I explicitly said that I do not want to use await but thanks for your answer.

    – Sebastian Szczepański
    Mar 9 at 7:35











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



);













draft saved

draft discarded


















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









0














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.






share|improve this answer























  • I explicitly said that I do not want to use await but thanks for your answer.

    – Sebastian Szczepański
    Mar 9 at 7:35















0














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.






share|improve this answer























  • I explicitly said that I do not want to use await but thanks for your answer.

    – Sebastian Szczepański
    Mar 9 at 7:35













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page