Last active
July 13, 2019 22:19
-
-
Save stephenquan/9938b95c81ab6ec4ec1d483dcc0cecf8 to your computer and use it in GitHub Desktop.
AppStudio_20190605_PromiseNesting.qml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import QtQuick 2.12 | |
import QtQuick.Controls 2.5 | |
import ArcGIS.AppFramework 1.0 | |
Item { | |
Button { | |
text: qsTr("Test Promise nesting") | |
onClicked: { | |
download( "GET", "https://community.esri.com/groups/appstudio" ) | |
.then( (data) => { | |
showTitle( data ) | |
download( "GET", "https://appstudio.arcgis.com" ) | |
.then( (data) => { | |
showTitle( data ) | |
download( "GET", "https://community.esri.com/groups/survey123" ) | |
.then( (data) => { | |
showTitle( data ) | |
} ) | |
.catch( handleError ) | |
} ) | |
.catch( handleError ) | |
} ) | |
.catch( handleError ) | |
} | |
} | |
function showTitle( networkRequest ) { | |
let responseText = networkRequest.responseText | |
let [ , title ] = responseText.match(/<title.*>(.*)<\/title>/) | |
console.log( title ) | |
} | |
function handleError(error) { | |
console.log( "Caught Exception: ", error.message ) | |
console.log( "Stack: ", error.stack ) | |
throw error | |
} | |
function download( method, url, ...form ) { | |
networkRequestWithPromise.method = method | |
networkRequestWithPromise.url = url | |
return networkRequestWithPromise.sendPromise( ...form ) | |
} | |
NetworkRequest { | |
id: networkRequestWithPromise | |
property var _resolve | |
property var _reject | |
uploadPrefix: "file://" | |
onReadyStateChanged: { | |
if (readyState !== 4) return | |
if (status < 200 || status >= 299) { | |
_reject( new Error( qsTr("Download failure %1: Status Code %2: %3").arg(url).arg(status).arg(statusText) ) ) | |
return | |
} | |
if (errorCode !== 0) | |
{ | |
_reject( new Error( qsTr("Download failure %1: Error Code %2: %3").arg(url).arg(error).arg(errorText) ) ) | |
return | |
} | |
_resolve( networkRequestWithPromise ) | |
} | |
function sendPromise( ...form ) { | |
return new Promise( function (resolve, reject) { | |
_resolve = resolve | |
_reject = reject | |
send( ...form ) | |
} ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment