0

I'm just learning promises, and am stuck with the following scenario. I'll use an example of building a house.

First, I want to build my house. Once my house is completely finished, I will start two projects at the same time: landscaping my yard, and building a pool. If I understand everything so far, that would be:

buildHouse()
    .then(function () {
        landscapeYard();
        buildPool();
    }

My problem is now I want to plant flowers only after the yard is completely landscaped. And I want to build a fence around my pool whenever my pool is completely built. Something like this? The pool and yard projects can go on separately, at different paces. Maybe the pool project will take 2 years to finish, and the yard project will take 1 week:

buildHouse()
    .then(function () {
        landscapeYard();
        .then(function () {
             plantFlowers();
         })

        buildPool();
        .then(function () {
             buildFence();
        })
    })

Is that correct?

And my final problem is, once BOTH projects are completely finished, I want to start an entirely new project. Thanks for any help.

KayakinKoder
  • 3,243
  • 3
  • 23
  • 37
  • 1
    [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). This question has been asked and answered many times – Phil Sep 21 '17 at 05:15
  • Yeah I've read numerous tutorials with promise.all but haven't found anything that makes it clear how to handle this situation with nested promises – KayakinKoder Sep 21 '17 at 05:20
  • You're missing `);` everywhere – Bergi Sep 21 '17 at 05:23
  • 2
    @KayakinKoder The answer to nested promises is always: `return` it from the callback! – Bergi Sep 21 '17 at 05:24
  • @Bergi I should've mentioned this is psuedo code. I do use return everywhere, I'm not sure how that would fix my problem/question though. – KayakinKoder Sep 21 '17 at 05:26
  • 2
    @KayakinKoder Well please post your actual code and your attempt at using `Promise.all`. I guess the problem is only pseudo-fixed as well? – Bergi Sep 21 '17 at 05:30
  • 1
    `buildHouse().then(() => Promise.all([landscapeYard(), buildPool()])) .then(([yard, pool]) => Promise.all([plantFlowers(yard), buildFence(pool)])).then(() => console.log("complete")).catch(err => console.error(err))` – guest271314 Sep 21 '17 at 05:33
  • Thanks guest, that's really helpful. Bergi what I have in my question is more or less my actual code; as you can probably tell, I have no idea where to even start with promise.all, so guest's comment was exactly what I was looking for; I needed to get a basic understanding of the concept. @guest271314 if you post that as an answer I'll accept. – KayakinKoder Sep 21 '17 at 05:41
  • 1
    @KayakinKoder `function buildHouse() {return Promise.resolve()} function landscapeYard() {return Promise.resolve("yard landscaped")} function buildPool() {return Promise.resolve("pool built")} function plantFlowers(yard) { return Promise.resolve(yard + " and flowers planted") } function buildFence(pool) {return Promise.resolve(pool + " and fence built")} buildHouse().then(() => Promise.all([landscapeYard(), buildPool()])) .then(([yard, pool]) => Promise.all([plantFlowers(yard), buildFence(pool)])).then(result => console.log(result)).catch(err => console.error(err))` Cheers – guest271314 Sep 21 '17 at 05:43

0 Answers0