0

There is a nested function where asnyc functions and normal functions are used. I am a beginner in JavaScript by the way.

async function run() {
  try {
    await Excel.run(async context => {
      document.getElementById("status").value = "";
      let checks = [];
      const sheet = context.workbook.worksheets.getActiveWorksheet();
      const uR = context.workbook.getSelectedRange();
      uR.load('values');
      await context.sync();
      let data2 =uR.values;
      let data2.forEach((row,index)=>{
      docfunction(row);

Within this docfunction I want to use:

   function docfunction(row) {
    let checknumber = context.workbook.functions.isNumber(row[5]);
    checknumber.load('value');
    await context.sync();
    checknumber = checknumber.value;

However, await context.sync() is not working.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • Can you add some more code as snippet. – chandan_kr_jha May 27 '20 at 17:08
  • 1
    I guess you must declare `docfunction` as async too, `async function docfunction(row) {...}`. So, when invoking that function use `await docfunction(row)`; One more thing, bear in mind context.sync() should be async (promise)! But, as the own function name suggests, `context.sync()` looks *sync*, so, just remove await from this line. – Rodrigo May 27 '20 at 18:30
  • Explain in more detail what you mean by "not working". context.sync returns a Promise and await should work as microsoft's docs show as example: https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/correlated-objects-pattern it is also recommended that you do not use sync in a loop – user120242 May 27 '20 at 19:46
  • let data2.forEach is also invalid syntax. are you just having trouble treating the data2 looping as async? – user120242 May 27 '20 at 19:51
  • thank you very much for the comments: syntax is fixed - i still get the error messsage: uncaught (in promise) richapi.error: before reading the property value, call the load method on the containing object. I need to say that docfunction has now the variables row and context. – Quant Analyst May 27 '20 at 19:55
  • And probably you'll need to [avoid `forEach`](https://stackoverflow.com/a/37576787/1048572) – Bergi May 27 '20 at 19:57
  • the trouble is within the function docfunction(row,context). Here the error message appears in the line checknumber = checknumber.value after the await context.sync(); – Quant Analyst May 27 '20 at 19:58
  • add console.log of those values or use the debugger and make sure it is actually those places throwing the error. console.log(checknumber.toJSON()) (this will attempt to output all properties and show `.load` msgs for elements not properly loaded) to see if it's really `checknumber` that's the problem. – user120242 May 27 '20 at 20:08

3 Answers3

0

you also need to make the docfunction async in order to make it work:

async function docfunction(row) {
let checknumber = context.workbook.functions.isNumber(row[5]);
checknumber.load('value');
await context.sync();
checknumber = checknumber.value;

then you need to call it with await keyword inside your run function:

await dysfunction(row);
Bhuwan Chandra
  • 294
  • 1
  • 9
0

You may be better served returning a list of Promises and using a for await of loop, but this should be what you are looking for.
You could make it work with a .forEach, but there is really no reason to as it does not make the code any more readable.

async function run() {
  try {
    await Excel.run(async context => {
      document.getElementById("status").value = "";
      let checks = [];
      const sheet = context.workbook.worksheets.getActiveWorksheet();
      const uR = context.workbook.getSelectedRange();
      uR.load('values');
      await context.sync();
      let data2 =uR.values;
      for(const [row,index] of data2.entries()) {
        await docfunction(row);

...

   async function docfunction(row) {
    let checknumber = context.workbook.functions.isNumber(row[5]);
    checknumber.load('value');
    await context.sync();
    checknumber = checknumber.value;
user120242
  • 14,918
  • 3
  • 38
  • 52
-1

await expect a promise, Here is dummy code for async/await.

async function run() {
    // It will wait for the promise to resolve
    await new Promise((resolve) => {
        setTimeout(() => {
            resolve(console.log('Inside promise'));
        }, 3000);
    });
    // when promise gets resolved it will print
    console.log('Outside Promise');
}

run();
VIKRAM
  • 52
  • 7