0

This is my code all of them work correctly except this: new forms().saveForm(item, null). It doesn't call in order, forms is prototype in an another js file and I called here. what should I do to call it in order?

This is my saveform function

  const _saveForm = function (model, serverId) {
        return new Promise((resolve, reject) => {
            try {
                $.ajax({
                    beforeSend: function () { loadPanel.indicatorShow(); },
                    complete: function () { loadPanel.indicatorHide(); },
                    method: 'POST',
                    url: '/Publish/AddForms',
                    data: { form: model, serverFormId: serverId },
                }).done((data) => {
                    if (data.HasError) {
                        reject({ state: 'error', content: data });
                    }
                    if (!data.HasError) {
                        resolve({ state: 'done', content: data });
                    }
                }).fail((errorThrown) => {
                    reject({ state: 'fail', content: errorThrown });
                });
            } catch (error) {
                reject({ state: 'error', content: error });
            }
       });

    }

const s_db = new storeDb();
s_db.getBasicFormDataFromDb().then((result) => {
                                return result;
                            }).then((basicForms) => {
                                s_db.getProviderIdsFromDb().then((providerIds) => {
                                    basicForms.forEach(async (item) => {
                                        var arr_field = [];
                                        const _form_fields = item['fields'];
                                        _form_fields.forEach(async fieldItem => {
                                            var jsondata = JSON.parse(fieldItem);
                                            if (jsondata.hasOwnProperty('SelectiveSetting')) {
                                                var replacePId = jsondata.SelectiveSetting.DataProvider.ProviderId;
                                                _.find(providerIds, (k) => {
                                                    if (k['oldId'] === replacePId) {
                                                        jsondata.SelectiveSetting.DataProvider.ProviderId = k['newId'];
                                                        fieldItem = JSON.stringify(jsondata);
                                                    }
                                                });
                                            }
                                            arr_field.push(fieldItem);
                                        });

                                        item['fields'] = arr_field;
                                        new forms().saveForm(item, null);
                                    })
                                })
                            })
zahra banaeian
  • 121
  • 1
  • 6
  • 99% of the time you use `async`/`await` in `forEach` it doesn't do what you expect - use a regular for loop instead – Bravo Aug 25 '21 at 10:42
  • @Bravo is correct, you are supplying an async function to your `forEach` Loop that simply get calls twice. And as the nature of asynchronous functions is, the then get executed asynchronously – Aram Becker Aug 25 '21 at 10:46
  • Not exactly what I sai, nothing to do with `.then` - everything to do with using async/await in forEach is 99% of the time a *bad idea™* – Bravo Aug 25 '21 at 10:49
  • Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – Bravo Aug 25 '21 at 10:49
  • @Bravo,thanks for your response, I used regular for but it didn't work – zahra banaeian Aug 25 '21 at 10:56
  • Typo?? `new forms().saveForm(item, null);` => `new forms()._saveForm(item, null);` Looks like you forgot the `_` in `_saveForm`.. – ikhvjs Aug 25 '21 at 11:31
  • @zahrabanaeian - not in the code you posted, so it's hard to tell what you did wrong – Bravo Aug 25 '21 at 13:45
  • @Bravo ok, let me describe in this way: Think you have an object that contains nested list like this: { "from": [ { "id": 6, "arr": [ 1, 2 ] }, { "id": 1, "arr": [ 10, 8 ] } ] } that should be insert in database in order but before the object inserted I should do something with my "arr" list then insert my object respectively, like at first item with id=6 insert then second item with id = 1 should be insert. – zahra banaeian Aug 26 '21 at 06:10
  • you can described it 10 ways from Sunday - doesn't change the fact that async/await in forEach never does what you want - – Bravo Aug 26 '21 at 06:17
  • @Bravo I understand. but I describe the problem to give me solution if you have – zahra banaeian Aug 26 '21 at 06:27
  • No, I have no solution to the code you posted in the question – Bravo Aug 26 '21 at 06:31

0 Answers0