question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird connections behaviour when using async functions

See original GitHub issue

Hi @jerosoler , thank you again for your great tool. I’m using Drawflow for an application that allows the storage of flows on a server with standard REST API. In my initial version, I ask the name of the flow to load using the Window.prompt() method and I use the returned name to fetch the document from server, then load the flow like this:

//
// this is part of the downloadApp() function
//
// data is the raw response from server
var df = JSON.parse(data);
editor.clear();
editor.drawflow = df;
editor.load();

and all works well. Replacing Window.prompt() with SWAL (Sweet Alert2) requires the use of an async function:

async function altPrompt(label) {
var retval = await (async () => {
  const { value: appId } = await Swal.fire({
      input: 'text',
      inputLabel: label,
      inputPlaceholder: 'flow name',
      inputAttributes: {
          'aria-label': 'Flow Name'
      },
      showCancelButton: true
  })
  if (appId != null && appId.length > 0) {
    return appId;
  } else {
    return null;
  }
  })() 
  return retval;
}

If I fetch the flow from the server in this way:

async function fetchApp() {
  // const applId = window.prompt('Application ID:'); <- this works well!
  const applId = await altPrompt('Application ID:');
  await downloadApp(applId);
}

Then the flow loads correctly but the connections aren’t. Connections appear as horizontal lines on top of the flow. I tried to call a refresh connection function like this:

function refreshConnections() {
  var df = editor.drawflow;
  var modules = df.drawflow;
  for (var module in modules) {
    var nodes = modules[module].data; // this is the array of node objects
    for (var node in nodes) {
      var dta = nodes[node];
      var key = dta.id;
      editor.updateConnectionNodes ('node-'+key)
    }
  }
}

but this only works if it’s invoked after some timeout, like this:

async function fetchApp() {
  // const applId = window.prompt('Application ID:');  <- this works well!
  const applId = await altPrompt('Application ID:');
  await downloadApp(applId)
  setTimeout(() => {         // meh
     refreshConnections();
   }, 100);
}

This clumsy workaround works most of times. Not always, though. Note that using Window.prompt() the connections are always good. Note also that manually forcing a redraw, like changing current module, also draws the connections correctly, without the need of the editor.updateConnectionNodes() method.

I’m probably doing something wrong because I’m still learning Javascript, but I can’t see what. Can you point me in the right direction?

Thanks Massimo

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mcesarocommented, Nov 29, 2021

I was able to determine that the problem lies in an interaction with https://sweetalert2.github.io/ I replaced this library with an equivalent for modal dialog (nor using the async functions like SWAL2), and the problem disappears.

0reactions
jerosolercommented, Nov 29, 2021

Oh excellent!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async await strange behaviour with functions - Stack Overflow
I not understand - in question example there is async function someWrapper() but that function don't return anything (it even doesn't have ...
Read more >
Strange behavior with Observable.Create<T>(async ... - GitHub
Hi, AsyncObservable is behaving as async because of await keyword, which basically returns the flow to the caller and then once Task.Delay(50) ...
Read more >
Weird Rust / Hyper / Async behaviour. Why ?? : r/rust - Reddit
I don't really have time to play with it, but here's some guesses: There may be a back-pressure problem, with requests being accepted...
Read more >
How to use promises - Learn web development | MDN
With a promise-based API, the asynchronous function starts the operation and returns a Promise object. You can then attach handlers to this ...
Read more >
Best Practices for ES2017 Asynchronous Functions (`async ...
Best Practices for ES2017 Asynchronous Functions (`async`/`await`) · Schedule first, await later · Avoid mixing callback-based APIs and promise- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found