Aufgaben-Python/courses/javascript/11_callbacks_und_promises/musterloesung.js

42 lines
978 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 🌐 JS 05: MUSTERLÖSUNG – CALLBACKS & PROMISES 🌐
* ===============================================
*/
function verzoegereAusfuehrung(ms, rueckgabeWert) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(rueckgabeWert);
}, ms);
});
}
function pruefeZahlGroesserNull(zahl) {
return new Promise((resolve, reject) => {
if (typeof zahl === "number" && zahl > 0) {
resolve(zahl);
} else {
reject(new Error("Zahl muss größer als 0 sein"));
}
});
}
function ladeBenutzerProfil(userId) {
return new Promise((resolve, reject) => {
if (typeof userId === "number" && userId > 0) {
setTimeout(() => {
resolve({
id: userId,
name: `Benutzer_${userId}`,
status: "aktiv"
});
}, 10);
} else {
reject(new Error("Ungültige User-ID"));
}
});
}
function ladeAlleDatenQuellen(quelleA, quelleB) {
return Promise.all([quelleA, quelleB]);
}