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.

engine.execute() does not wait all the async service to be resolved

See original GitHub issue

Hi, I have a chart like below, each service will run an async function. I expect they will be run one by one, and then finish. But what I get is it finished before the second service is done. Do I mis-understand something which is basic?

console output

### before execute
### before serviceFn1 await
### after serviceFn1 await
### before serviceFn2 await
### after execute  // I expect this line will be at the last line, but it isn't.
### after serviceFn2 await
### inside execute callback

chart

diagram_1

sample code

const { Engine } = require('bpmn-engine');

const source = `
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions id="Definitions_0cz8kjv" targetNamespace="http://bpmn.io/schema/bpmn"
	xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
  <bpmn:process id="Process_0x60oya" isExecutable="true">
    <bpmn:startEvent id="StartEvent">
      <bpmn:outgoing>Flow1</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow1" sourceRef="StartEvent" targetRef="ServiceTask1" />
    <bpmn:serviceTask id="ServiceTask1" implementation="\${environment.services.serviceFn1}" >
      <bpmn:incoming>Flow1</bpmn:incoming>
      <bpmn:outgoing>Flow2</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="Flow2" sourceRef="ServiceTask1" targetRef="ServiceTask2" />
    <bpmn:serviceTask id="ServiceTask2" implementation="\${environment.services.serviceFn2}" >
      <bpmn:incoming>Flow2</bpmn:incoming>
      <bpmn:outgoing>Flow3</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="EndEvent">
      <bpmn:incoming>Flow3</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow3" sourceRef="ServiceTask2" targetRef="EndEvent" />
  </bpmn:process>
</bpmn:definitions>
`;

(async () => {
	const engine = Engine({ source });
	console.log('### before execute');
	await engine.execute({
		services: {
			serviceFn1: async (scope, callback) => {
				console.log('### before serviceFn1 await');
				await (async () => {})();
				console.log('### after serviceFn1 await');
				return callback(null, 12345);
			},
			serviceFn2: async (scope, callback) => {
				console.log('### before serviceFn2 await');
				await (async () => {})();
				console.log('### after serviceFn2 await');
				return callback(null, 23456);
			},
		},
	}, () => {
		console.log('### inside execute callback');
	});
	console.log('### after execute');
})();

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
paed01commented, Aug 14, 2020

That was true for an older version of the engine. I have updated the documentation.

0reactions
aowakennomaicommented, Aug 14, 2020

Hi, thank you for the advice, I’ll try to think more about event-driven programing 😃

By the way, I found that it says that

Event names are composed by activity event name and activity id, e.g. wait-userTask.

at here Execution listener. Is that mean I can listen like this? (which I’ve tried but not work…)

listener.on('wait-MyTaskId', (eleApi, engApi) => {});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't the code after await run right away? Isn't it ...
Just as its name implies, the await keyword will cause the function to "wait" until its promise resolves before executing the next line....
Read more >
Nodejs does not wait for promise resolution - exits instead
js process will exit when there is no work scheduled, but a listener registered on the 'beforeExit' event can make asynchronous calls, and ......
Read more >
Async Methods - Testing Library
Several utilities are provided for dealing with asynchronous code. These can be useful to wait for an element to appear or disappear in ......
Read more >
Control startup and shutdown order in Compose
However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it's...
Read more >
javascript - await does not wait for Promise to finish
This is standard for asynchronous events in JavaScript. async functions always return a Promise which is not guaranteed to be resolved before ...
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