question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Await chain like promise chain

See original GitHub issue

I got an situation here.

My team is using await in a page that we need to handle more then one Promise They tried to use something like this

<await(data from firstProvider)>
    <await(complementData from secondProvider)>
    </await>
</await>

They said to me they got some errors with nested await and i was thinking in two solutions for this scenario

I was thinking about wrap the Promises in an especifc provider and use Promise.all(requests)

or create a custom tag like this one <await-all(data from [Promise1, Promise2, ....])</await-all>

What is the best solution to implement something like this?

What do you think about it??

Thanks and i’m in love with marko 🚀

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
mauricionrcommented, Feb 25, 2017

Here’s my demo/test about this issue

https://github.com/mauricionr/marko-demo

<var watchers=Promise.all([data.promise1, data.promise2]) />

the service

module.exports = () => {
    return new Promise((resolve, reject) => {
        request.get(config.categories, (error, response, body) => {
            setTimeout(() => {
                resolve(JSON.parse(body));
            }, 5000)
        })
    })
}

the controller

module.exports = (req, res) => {
    template.render({
       name:'Mauricio Nunes dos reis',
       colors:['red', 'green', 'blue'],
       promise1:categorieService(),
       promise2:categorieService()
   }, res)
}

the view

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Marko Demo</title>
    </head>
    <body>
        <h1>Marko Demo</h1>
        <h2>${data.name}</h2>
        <section>
            <ul>
                <li for(color in data.colors)>${color}</li>
            </ul>
        </section>
        <var watchers=Promise.all([data.promise1, data.promise2]) />
        <await(response from watchers) client-reorder=true>
            <await-placeholder>
                Loading data...
            </await-placeholder>
            <await-error>
                An error occurred!
            </await-error>
            <await-timeout>
                A timeout occurred!
            </await-timeout>

            <h3>Promise1</h3>
            <section>
                <ul>
                    <li for(cat in response[0])>${cat.name}</li>
                </ul>
            </section>
            <h3>Promise2</h3>
            <section>
                <ul>
                    <li for(cat2 in response[0])>${cat2.name}</li>
                </ul>
            </section>
        </await>
        <await-reorderer/>
    </body>
</html>

Works well 🚀

But i could not see the Loading data and i’m using the tag await-placeholder

Since i’m using json-server to fake and json api, i put an timeout to looks like the real world and could not see the loading.

Any guess?

Thanks guys!!

0reactions
mauricionrcommented, Mar 10, 2017

@mlrawlings yes, but you can double check if really is server-side rendering here’s the demo

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using await in promise chain - javascript - Stack Overflow
yes, await can only be used inside a function that is async - a function inside an async function isn't async unless it...
Read more >
Promises chaining - The Modern JavaScript Tutorial
Looks like promise chaining is meant to replace async/await structure. If you need to wait for something then just wrap it in a...
Read more >
Promise chaining is dead. Long live async/await
In this article, we will look at how async/await really makes developers' lives easier and why you should stop using promise chaining.
Read more >
Migrating from Promise chains to Async/Await - AfterAcademy
Introduces Async/Await and compare it with Promises. We shall discuss migrating your from Promise Chains to Async/Await .
Read more >
Promises, Promise Chain and Async/Await - Time to Hack
Promises, Promise Chain and Async/Await ⏳ Callbacks were the old ways to handle asynchronous code execution; but with Promises, Promise Chains ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found