-
Notifications
You must be signed in to change notification settings - Fork 4
Example Promise Chain
Stephen Quan edited this page Nov 16, 2022
·
2 revisions
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
ListView {
anchors.fill: parent
clip: true
model: ListModel {
id: _console
function appendMsg(msg,col) {
append({msg,col,ts:Date.now()});
}
function log(...params) {
console.log(...params);
appendMsg(params.join(" "),"black");
}
function error(...params) {
console.log(...params);
append(params.join(" "),"red");
}
}
delegate: Frame {
width: ListView.view.width - 20
background: Rectangle {
color: index & 1 ? "#eee" : "#ccc"
}
RowLayout {
width: parent.width
Text {
text: index + 1
color: "grey"
}
Text {
Layout.fillWidth: true
text: msg
color: col
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
}
Text {
text: Qt.formatTime(new Date(ts),"h:mm:ss ap")
color: "grey"
}
}
}
ScrollBar.vertical: ScrollBar {
width: 20
policy: ScrollBar.AlwaysOn
}
}
footer: Frame {
RowLayout {
width: parent.width
Button {
text: qsTr("Simple Promise Chain")
onClicked: runAsync()
}
}
}
function runAsync() {
Promise.resolve(1001)
.then( (result) => {
_console.log(result);
return 2001;
})
.then( (result) => {
_console.log(result);
return 3001;
})
.then( (result) => {
_console.log(result);
})
.catch( (err) => {
let scope = [ err.fileName, err.lineNumber ];
if (err.columnNumber) scope.push(err.columnNumber);
_console.error(scope.join(":") + ": " + err.message);
})
;
}
}
You can Try it Online!