Last active
July 11, 2019 04:21
-
-
Save stephenquan/974ed4a2266e93b0eaa1105fc262de8d to your computer and use it in GitHub Desktop.
AppStudio_20190605_PromiseAsyncAwait.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 async/await" ) | |
onClicked: { | |
(async () => { | |
try { | |
console.log( ( await download( "GET", "https://community.esri.com/groups/appstudio") ).title ) | |
console.log( ( await download( "GET", "https://appstudio.arcgis.com") ).title ) | |
console.log( ( await download( "GET", "https://community.esri.com/groups/survey123") ).title ) | |
} catch ( error ) { | |
console.log( error ) | |
} | |
})() | |
} | |
} | |
function download( method, url, ...form ) { | |
return new Promise( (resolve, reject) => { | |
downloadComponent.createObject( app, { resolve, reject, method, url, form } ) | |
} ) | |
} | |
Component { | |
id: downloadComponent | |
NetworkRequest { | |
property var resolve | |
property var reject | |
property var form | |
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) ) ) | |
destroy() | |
return | |
} | |
if (errorCode !== 0) | |
{ | |
reject( new Error( qsTr("Download failure %1: Error Code %2: %3").arg(url).arg(error).arg(errorText) ) ) | |
destroy() | |
return | |
} | |
let data = { | |
url: url, | |
status: status, | |
title: ( responseText.match(/<title.*>(.*)<\/title>/) || [ ] ) [1], | |
responseText: responseText | |
} | |
resolve( data ) | |
destroy() | |
} | |
Component.onCompleted: send( ...form ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment