0

I've been having trouble working properly with Promises for a while now. I use another one in a Promise. Expected result: 1, 2, 3, 4 Actual result: 1, 2, 4, 3

api.get("orders/2049103")
    .then(({data}) => {
        userPayload.number = data.number;
        userPayload.first_name = data.billing.first_name;
        userPayload.last_name = data.billing.last_name;
        userPayload.email = data.billing.email;

        console.log(1);

        return data;
    })
    .then((data) => {
        console.log(2);
        let productInfo = {};
        productInfo.id = data.line_items[0].product_id;
        productInfo.name = data.line_items[0].name;

        api.get(`products/${productInfo.id}`)
            .then(({data}) => {
                productInfo.downloads = data.downloads;
                console.log(3);
            })
        return productInfo
    })
    .then(() => {
        console.log(4);
    })
OliverKhl
  • 49
  • 6

1 Answers1

0

Problem solved, thanks @bergi!

api.get("orders/2049103")
    .then(({data}) => {
        userPayload.number = data.number;
        userPayload.first_name = data.billing.first_name;
        userPayload.last_name = data.billing.last_name;
        userPayload.email = data.billing.email;
        userPayload.downloads = [];

        console.log(1);

        return data;
    })
    .then((data) => {
        console.log(2);
        let productInfo = {};
        productInfo.id = data.line_items[0].product_id;
        productInfo.name = data.line_items[0].name;

        return api.get(`products/${productInfo.id}`)
            .then(({data}) => {
                productInfo.downloads = data.downloads;
                userPayload.downloads.push(productInfo);
            })
    })
    .then(() => {
        console.log(userPayload);
    })
OliverKhl
  • 49
  • 6