javascript - How to yield value multiple times from function? - Stack Overflow

So what I am doing is, I have 2 files, One that contain a script which would generate a token and the s

So what I am doing is, I have 2 files, One that contain a script which would generate a token and the second file handle that token.

The problem is that the second script which would log the token it would only log the first token received.

This is the how I am handling the token:

const first_file = require("./first_file.js");
first_file.first_file().then((res) => {
    console.log(res);
});

And clearly that wouldn't work, Because it's not getting updated with the newer value.

first_file = async () => {
    return new Promise(async (resolve, reject) => {
        //Generating the token
        (async () => {
            while (true) {
                console.log("Resolving...");
                resolve(token);
                await sleep(5000);
                resolved_token = token;
            }
        })();
    });
};

module.exports = { first_file };

What I am doing here is, I tried to do a while..loop so that I keep resolving the token. But it didn't, Is there and way I can export the variable directly so the task would be easier ?

So what I am doing is, I have 2 files, One that contain a script which would generate a token and the second file handle that token.

The problem is that the second script which would log the token it would only log the first token received.

This is the how I am handling the token:

const first_file = require("./first_file.js");
first_file.first_file().then((res) => {
    console.log(res);
});

And clearly that wouldn't work, Because it's not getting updated with the newer value.

first_file = async () => {
    return new Promise(async (resolve, reject) => {
        //Generating the token
        (async () => {
            while (true) {
                console.log("Resolving...");
                resolve(token);
                await sleep(5000);
                resolved_token = token;
            }
        })();
    });
};

module.exports = { first_file };

What I am doing here is, I tried to do a while..loop so that I keep resolving the token. But it didn't, Is there and way I can export the variable directly so the task would be easier ?

Share Improve this question edited Apr 25, 2021 at 1:19 Nur 2,4933 gold badges18 silver badges35 bronze badges asked Apr 24, 2021 at 14:59 DUMBUSERDUMBUSER 5519 silver badges25 bronze badges 4
  • sorry for the bad question just don't know how to explain what I want – DUMBUSER Commented Apr 24, 2021 at 15:00
  • What is token, are you fetching it from somewhere? – Som Shekhar Mukherjee Commented Apr 24, 2021 at 15:13
  • You don't need to wrap inline async function for while loop in order to use await keyword. Also for returning Promise object – Nur Commented Apr 24, 2021 at 16:38
  • Never pass an async function as the executor to new Promise! And that IIFE seems pointless as well. – Bergi Commented Apr 25, 2021 at 2:16
Add a ment  | 

1 Answer 1

Reset to default 8

If I understand your question correctly, You want to resolve promise multiple times, And It's nothing to do with modules...

But You understand something wrong about promise in JavaScript...
You can't resolve a promise twice.

Generator

But you can generate new value from function, this type of function also known as generator, Where a function can reenter its context (Something like async/await) and yield result using yield keyword.

Usually a generator is used in for..of loop. It has next() method for yield next value from a generator...

Lets look an example:

const delay = ms => new Promise(res => setTimeout(res.bind(null, ms), ms));

async function* generator() {
    yield 'yield result from generator!'
    for (let ms = 100; ms <= 300; ms += 100) {
        yield 'delay: ' + await delay(ms) + ' ms';
    }
    yield delay(1000).then(() => 'you can also yield promise!');
}

async function main() {
    const gen = generator();
    console.log('1st', (await gen.next()).value);
    for await (const ms of gen) {
        console.log(ms)
    }
}

main()

Note that * after function, So that we know that this function a generator, with async keyword this is Async Generator.

Generator is very useful. like: Generate value on demand, Pass data like pipe!, Can return endless value from function etc...

Callback

This old school method heavily used in node, Where you pass a callback function as argument.

Example:

const delay = ms => new Promise(res => setTimeout(res.bind(null, ms), ms));

async function callback(fn) {
    fn('yield result from callback!');
    for (let ms = 100; ms <= 300; ms += 100) {
        fn('delay: ' + await delay(ms) + ' ms');
    }
    await delay(1000);
    fn('yield asynchronously!');
}

callback(value => console.log(value));

This approach create all sort of nutsy problem, like: created function scope, disaster of control flow, doesn't have break keyword etc... I don't remend this method.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745335007a4623062.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信