1

I have this code:

function donwloadFromBucket() {
    const {Storage} = require('@google-cloud/storage');
    const storage = new Storage();
    const options = {  destination: "c:/tmp/dw.js", };

  await storage
    .bucket("pipelines-models")
    .file("index.js")
    .download(options);  }
}

donwloadFromBucket();

nextOperations();

I want the donwloadFromBucket function to complete and only then to proceed to the function nextOperations.

How do i achieve this ?

1
  • 1
    async main function(){ donwloadFromBucket(); nextOperations(); } main() Commented Jun 9, 2019 at 12:42

1 Answer 1

2

Try something like this ,in second async function ,next oprations won't execute unless donwloadFromBucket(); completes.Also you have can use await only in async function,your donwloadFromBucket(); is not async.

 async function donwloadFromBucket() {
        const {Storage} = require('@google-cloud/storage');
        const storage = new Storage();
        const options = {  destination: "c:/tmp/dw.js", };

    return await storage  //  return something here ,to make the function finish completion.
        .bucket("pipelines-models")
        .file("index.js")
        .download(options);  }
    }


    async function second()
    {
    await donwloadFromBucket();

    nextOperations();

    }
 second(); // call the second function

Something note worthy-

An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. Follow up Link

Sign up to request clarification or add additional context in comments.

2 Comments

i think the first async await is useless
Well that's how it is mentioned in the question.He has to make it async to achieve what he is looking otherwise ,event loop will execute it accordingly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.