Goal: Awaiting multiple queries one after each other in a JS query
Steps:
- created 3 Google Cloud Storage (GCS) queries: getFileA, getFileB, getFileC
- created 1 REST query: importFiles
- created a JS query: handleImportFiles
getFileA, getFileB, getFileC simply read dedicated files from Google Cloud Storage
importFiles simply invokes a REST API
handleImportFiles should trigger the queries one after each other:
// handleImportFiles JS Query code
await getFileA.trigger();
await getFileB.trigger();
await getFileC.trigger();
const res = await importFiles.trigger();
console.warn(res);
Details:
I expect handleImportFiles to exit and fail with an error if any of getFileA, getFileB, getFileC or importFiles fails.
Now importFiles fails due to a HTTP 400 error. importFiles.data
reflects this after importFiles
ran as follows:
{
"statusCode": 400,
"error": "Bad Request",
"message": "{\"code\":\"SCHEMA_INVALID\",\"message\":\"Manufacturer schema invalid.\"}",
"data": {
"code": "SCHEMA_INVALID",
"message": "Manufacturer schema invalid."
}
}
But handleImportFiles does not exit and fail. Instead, the console.warn
outputs undefined
.
Why is that?
Changing handleImportFiles
as follows:
// handleImportFiles JS Query code
await getFileA.trigger();
await getFileB.trigger();
await getFileC.trigger();
const res = await importFiles.trigger({onFailure: (e) => {
console.error (e);
}});
console.warn(res);
Yields an console error with the actual error response:
{"name":"bt","data":{"statusCode":400,"error":"Bad Request","message":"{\"code\":\"SCHEMA_INVALID\",\"message\":\"Manufacturer schema invalid.\"}","data":{"code":"SCHEMA_INVALID","message":"Manufacturer schema invalid."},
But still undefined
is logged to console and handleImportFiles
does not exit with failure.
Why is that?
Using .then()
also does not work:
// handleImportFiles JS Query code
await getFileA.trigger();
await getFileB.trigger();
await getFileC.trigger();
const res = await importFiles.trigger()
.then((data) => {console.warn(data); return data;});
console.warn(res);
It results in two undefined
s in the console.
Only when wrapping the query trigger in a Promise
, the code works as expected:
await getFileA.trigger();
await getFileB.trigger();
await getFileC.trigger();
const res = await new Promise((resolve, reject) => {
importFiles.trigger({
onSuccess: (data) => resolve(data),
onFailure: (e) => reject(e)
});
});
console.warn (res);
The handleImportFiles
query fails and no undefined
is logged to console.
Why does all the other code not yield the expected results of a failing handleImportFiles
JS Query?
It can't be that we have to wrap each single query trigger in a dedicated promise, right?
Thank you for your support