A cikin aiwatar da haɓaka aikace-aikacen Node.js, fahimta da aiki tare da sarrafa taron da sarrafa asynchronous suna da mahimmanci. Node.js an gina shi akan ƙirar abin da ba a so da kuma asynchronous, yana ba da damar aiwatar da ayyuka ba tare da jiran kammalawa ba. A zahiri, fahimta da yin amfani da daidaitaccen sarrafa taron da sarrafa asynchronous wani muhimmin sashi ne na inganta aikin aikace-aikacen.
Abubuwan da suka faru da callback s a cikin Node.js
A cikin Node.js, abubuwan da suka faru da callback s suna taka muhimmiyar rawa wajen sarrafa ayyukan asynchronous. Abubuwan da suka faru hanya ce ta kulawa da amsa wasu ayyuka ko abubuwan da suka faru a cikin aikace-aikacen. Callback s, a gefe guda, ayyuka ne waɗanda ake aiwatar da su da zarar wani takamaiman lamari ko aiki ya ƙare.
Node.js yana ba da tsarin gine-ginen da aka gudanar inda sassa daban-daban na aikace-aikacen zasu iya fitar da abubuwan da suka faru kuma su saurare su. Wannan yana ba da damar ingantaccen aiki da rashin toshe aiki na ayyuka da yawa a lokaci guda.
Callback s ana yawan amfani da su a cikin Node.js don gudanar da ayyukan asynchronous. Ana wuce su azaman muhawara zuwa ayyuka kuma ana aiwatar da su da zarar an gama aikin. Callback s samar da hanya don sarrafa sakamako ko kurakurai waɗanda ke faruwa yayin ayyukan asynchronous.
Ga misali na amfani da a callback Node.js:
// A function that takes a callback
function fetchData(callback) {
// Simulate fetching data from an asynchronous operation
setTimeout(() => {
const data = { name: 'John', age: 30 };
callback(null, data); // Pass the data to the callback
}, 2000); // Simulate a 2-second delay
}
// Call the fetchData function and provide a callback
fetchData((error, data) => {
if(error) {
console.error('Error:', error);
} else {
console.log('Data:', data);
}
});
A cikin wannan misali, muna da wani aiki da ake kira fetchData
wanda ke kwaikwayi debo bayanai daga aiki asynchronous(misali, yin kiran API ko neman bayanai). Yana ɗaukar callback aiki azaman hujja.
A cikin fetchData
aikin, muna amfani setTimeout
da simintin aikin asynchronous. Bayan jinkiri na 2 na biyu, muna ƙirƙira wasu bayanan samfurin kuma mu wuce shi zuwa callback aikin tare da kuskure(wanda aka saita null
a wannan yanayin).
A wajen fetchData
aikin, muna kiran shi kuma muna ba da callback aiki. A cikin callback, muna sarrafa kowane kuskuren kuskure kuma muna sarrafa bayanan da aka karɓa. Idan akwai kuskure, muna shiga cikin na'ura mai kwakwalwa. In ba haka ba, muna shiga bayanan.
Wannan babban misali ne na yin amfani da a callback cikin Node.js don gudanar da ayyukan asynchronous da tabbatar da sarrafa bayanan da zarar ya samu. A cikin yanayi na ainihi, callback ana amfani da s don sarrafa tambayoyin bayanai, buƙatun API, da sauran ayyuka masu kama da juna.
Amfani Promise da s da kuma sarrafa asynchronicity async/await
"Amfani Promise da sarrafa ayyukan asynchronous" hanya ce ta gama gari a cikin Node.js don gudanar da ayyukan asynchronous ta hanya mai sauƙi da inganci. wani abu ne da ke taimaka mana sarrafawa da sarrafa ayyukan asynchronous, yayin da shi ne syntax wanda ke ba mu damar rubuta lambar asynchronous ta hanyar kama da lambar daidaitawa. async/await Promise JavaScript async/await
Ta amfani Promise da, za mu iya rubuta lambar asynchronous cikin sauƙi da fahimta. Ba mu ƙara buƙatar yin amfani da ayyuka da ma'amala da jahannama( ayyukan kafa) don gudanar da ayyukan asynchronous. Madadin haka, zamu iya amfani da kalmar jira don jira don kammalawa da dawo da sakamakonsa. async/await callback callback callback Promise
Anan akwai misalin amfani Promise da kuma a cikin Node.js don gudanar da ayyukan asynchronous: async/await
// A mock function to fetch data from an API
function fetchData() {
return new Promise((resolve, reject) => {
setTimeout(() => {
const data = { name: 'John', age: 30 };
resolve(data); // Return data within the Promise
}, 2000);
});
}
// Using async/await to handle asynchronous operations
async function getData() {
try {
const data = await fetchData(); // Wait for the Promise to complete and return the data
console.log('Data:', data);
} catch(error) {
console.error('Error:', error);
}
}
// Call the getData function
getData();
A cikin wannan misalin, muna amfani da fetchData
aikin don kwaikwayi tattara bayanai daga API(ko duk wani aiki na asynchronous). Wannan aikin yana dawo da Promise, inda muke kiran resolve
aikin don dawo da bayanai.
A wajen fetchData
aikin, muna amfani da try/catch
toshe don magance kurakurai. A cikin getData
aikin, muna amfani da await
kalmar maɓalli don jira don Promise kammalawa da dawo da bayanan. Idan akwai kuskure a cikin Promise, zai jefa banda kuma muna sarrafa shi a cikin catch
toshe.
A ƙarshe, muna kiran getData
aikin don fara aikin asynchronous. Za a shigar da sakamakon a cikin na'ura wasan bidiyo bayan an Promise gama kuma dawo da bayanan.
Amfani Promise da kuma sa lambar mu ta zama abin karantawa da sauƙin fahimta yayin da ake mu'amala da ayyukan asynchronous. Yana taimaka mana mu guje wa jahannama kuma yana ba mu damar rubuta lamba a jere, kama da rubuta lambar aiki tare. async/await callback
Ƙarshe: Gudanar da taron da sarrafa asynchronous abubuwa ne masu mahimmanci guda biyu a cikin haɓaka aikace-aikacen Node.js. Ta hanyar fahimta da daidai yin amfani da ra'ayoyi da kayan aiki masu alaƙa, zaku iya gina ingantaccen aiki, sassauƙa, kuma amintaccen aikace-aikace akan dandalin Node.js.