Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

async await - making asynchronous calls synchronous in typescript

I have a situation in azure functions, where i am calling a api which returns 500items. Once i received them i have 500 items in a array. For each item i need to check if that item exist in list then update it else create new item with customID column which is calculated by them (latest item Id in list + 1).

But when i am doing it is itself batching all update and create request and executing them at once, which is causing same customID creation and saving in list at once.

Please help.

response.data.data.map(async (d) => {
                           
       if (d.attributes.strand !== 0) {

                                //#region  add in Strand list for attributes
                                await checkIFFPIExistInStrandList(context, d).then(async
                              (checkIFFPIExistInProgListVal) => {
                                    if (checkIFFPIExistInProgListVal === true) {
                                        // itemsToUpdate_Strand.push(d);
                                        await updateItemTo_StrandList(context, d);
                                    }
                                    else if (checkIFFPIExistInProgListVal === false) {
                                await createCustomID_Strand(context,process.env.CILS_Strand_ListName)
                                          .then(async (custStrandId) => {
                                         await addItemTo_StrandList(context,d,custStrandId);
                                        });
                                    } 
                                });
       else if (d.attributes.strand === 0) {
                                   
         //#region  add in Programme list for attributes
           await checkIFFPIExistInProgList(context, d).then(async (checkIFFPIExistInProgListVal) => {
                                    if (checkIFFPIExistInProgListVal === true) {
                                        await updateItemTo_ProgramList(context, d);
                                    }
                                    else if (checkIFFPIExistInProgListVal === false) {
                 await createCustomID_Program(context, process.env.CILS_ProgramTitle_ListName)
                               .then(async (custProgId) => {
                                                await addItemTo_ProgramList(context, d, custProgId);
                                            });
                                    }
                                });


async function checkIFFPIExistInProgList(context, itm): Promise<boolean> {
    let exist: boolean = false;

    l_ProgrammeLstItmsArr.map(async (data) => {
        
        if (data.CILSNo === itm.attributes.cils_no && data.FPINo === itm.id) {
            exist = true;
        }
        else {
            exist = false;
        }
    });

    return exist;
}

async function createCustomID_Program(context, listName): Promise<number> {

    let CustID = null;
    await sp.web.lists.getByTitle(process.env.CILS_ProgramTitle_ListName).items.select('ID').orderBy('ID', false).top(1).get()
        .then((Items: any) => {
            if (Items.length > 0) {
                console.info("createCustomID method success : " + Items.length);
                CustID = (Items[0].ID) + 1;
            }
            else {
                CustID = 0;
            }
        })
        .catch((err) => {
            console.error("Error in function createCustomID() : " + err);
            context.log.error("Error in function createCustomID() : " + err);
        });
    return CustID;
}


async function updateItemTo_ProgramList(context, itm): Promise<void> {

    await sp.web.lists.getByTitle(process.env.CILS_ProgramTitle_ListName)
        .items.top(1)
        .filter(`substringof('${itm.id}',FPINo) and substringof('${itm.attributes.cils_no}',CILSNo)`)
        // .filter("FPINo eq '" + itm.id + "'")
        .get()
        .then(async (Item: any) => {
            if (Item.length > 0) {
                await sp.web.lists.getByTitle(process.env.CILS_ProgramTitle_ListName)
                    .items.getById(Item[0].Id)
                    .update({
                        Title: (itm.attributes.cils_title !== undefined && itm.attributes.cils_title !== '') ? itm.attributes.cils_title : null,  //Singleline text
                       
                    })
                    .then(() => {
                        console.log("Item updated through  updateItemTo_ProgramList() function.");
                        context.log.info("Item updated through  updateItemTo_ProgramList() function.");
                    })
                    .catch((e) => {
                        console.error("Error in updateItemTo_ProgramList() function." + e);
                        context.log.error("Error in updateItemTo_ProgramList() function." + e);
                    });
            }
            else if (Item.length === 0) {

            }
        })
        .catch((e) => {
            console.error("Error in updateItemTo_ProgramList() function." + e);
            context.log.error("Error in updateItemTo_ProgramList() function : " + e);
        });
}

async function addItemTo_ProgramList(context, itm, custProgId) {
    await sp.web.lists
        .getByTitle(process.env.CILS_ProgramTitle_ListName)
        .items.add({
            ProgID0: custProgId,
            Title: (itm.attributes.cils_title !== undefined && itm.attributes.cils_title !== '') ? itm.attributes.cils_title : null,  //Singleline text
           
        })
        .then(() => {
            console.log("Item updated through  addItemTo_ProgramList() function.");
            context.log.info("Item updated through  addItemTo_ProgramList() function.");
        })
        .catch((e) => {
            console.error("Error in addItemTo_ProgramList() function." + e);
            context.log.error("Error in addItemTo_ProgramList() function." + e);
        });
}

                                 
question from:https://stackoverflow.com/questions/65923976/making-asynchronous-calls-synchronous-in-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...