添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
爱健身的啄木鸟  ·  overflow-y: auto ...·  5 月前    · 
风度翩翩的斑马  ·  matlab ...·  6 月前    · 

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In the below code I am making a Server Call using a async/await. Once I get the results I want to process the results and then move along after the .Then statement. The problem seems to be that Promise is still Pending so it is skipping the .Then and running the console.Log first after the .Then. How can I get the code to wait?

export function onBeforePriceRules(quote, lineModels, conn) {
let myServerResults = GetData(conn);
let ProcessResults = myServerResults;
console.log(ProcessResults) // Promise { <pending> }
ProcessResults.then(function(result) {
//Do stuff with the Results. 
console.log(result);
debugger; 
 console.log("New Code Block  = This is fireing before my .Then Statement because Promise is still Pending But I am using async and await");
debugger; 
return Promise.resolve();
async function GetData(conn) {
// Await for the query results
return await conn.query("SELECT Id, Model__c FROM Product2")
.then(returnedRecords => { return returnedRecords } )

The code is behaving appropriately, and the console.log at line 14 should fire first as per the lifecycle of promise in JS. If you have to stop the code at line number 3 (before it goes to line 14) then you should make a few changes.

Your code should be similar to this (note I did not run this code to test it)

async function someMethod(){
    let myServerResults = await GetData(conn);
    If(myServerResults){
        let ProcessResults = myServerResults;
        console.log(ProcessResults);
        let someOtherVar = await someOtherPromise();
        console.log(someOtherVar);
        //Do stuff with the someOtherVar. 
        debugger; 
this.someMethod();

Please refer this for further information.

Interesting. That makes sense. I feel like if you have to have multiple server calls this could get very hard to read. – CodeMonger Feb 3, 2022 at 3:16