تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
من المهم إدارة مراحل نشاط الدالة لضمان حلّها بشكل سليم. من خلال إنهاء الدوال بشكل صحيح، يمكنك تجنُّب الرسوم المفرطة
من الدوال التي تعمل لفترة طويلة جدًا أو تتكرر بلا حدود. يمكنك أيضًا التأكّد من أنّ مثيل Cloud Functions الذي يشغّل الدالة لا يتم إيقافه قبل أن تصل الدالة بنجاح إلى شرط أو حالة الإنهاء.
استخدِم الأساليب المقترَحة التالية لإدارة دورة حياة الدوال:
يمكنك حلّ الدوال التي تنفّذ معالجة غير متزامنة (المعروفة أيضًا باسم "دوال الخلفية") من خلال عرض وعد
JavaScript.
يمكن إنهاء دوال HTTP باستخدام res.redirect() أو res.send() أو res.end().
إنهاء دالة متزامنة باستخدام عبارة return;
تبسيط الرمز غير المتزامن باستخدام الوعود في JavaScript
تُعدّ الوعود بديلاً عصريًا لعمليات معاودة الاتصال في الرموز غير المتزامنة. يمثّل الوعد عملية والقيمة المستقبلية التي قد يعرضها. ويتيح لك أيضًا نشر أخطاء مشابهة لعبارة try/catch في الرمز المتزامن. يمكنك الاطّلاع على معلومات حول الوعود في حزمة تطوير البرامج (SDK) من Firebase على مدوّنة Firebase، وحول الوعود بشكل عام على شبكة مطوّري Mozilla.
طريقة عمل الوعود مع الدوال
عند إرجاع وعد JavaScript إلى دالة، تستمر هذه الدالة في العمل إلى أن يتم تنفيذ الوعد أو رفضه. للإشارة إلى أنّ إحدى الدوال قد أكملت عملها بنجاح، يجب أن يتم حلّ الوعد. للإشارة إلى حدوث خطأ، يجب رفض الوعد. وهذا يعني أنّه عليك فقط معالجة الأخطاء التي تريد معالجتها.
يأخذ الرمز التالي Firebase Realtime Databaseref ويضبط قيمته على "world!". من خلال عرض نتيجة set، سيتم ضمان استمرار تشغيل الدالة إلى أن يتم إكمال عملية كتابة السلسلة في قاعدة البيانات بشكل كامل:
تاريخ التعديل الأخير: 2025-08-23 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-23 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["\u003cbr /\u003e\n\nIt's important to manage the lifecycle of a function to ensure that it resolves\nproperly. By terminating functions correctly, you can avoid excessive charges\nfrom functions that run for too long or loop infinitely. Also, you can make sure\nthat the Cloud Functions instance running your function does not shut down\nbefore your function successfully reaches its terminating condition or state.\n\nUse these recommended approaches to manage the lifecycle of your functions:\n\n- Resolve functions that perform **asynchronous** processing (also known as \"background functions\") by returning a [JavaScript\n promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).\n- Terminate **HTTP functions** with `res.redirect()`, `res.send()`, or `res.end()`.\n- Terminate a **synchronous** function with a `return;` statement.\n\n| **Caution:** In all cases, be careful to avoid any situation in which the function's result actually retriggers the function --- for example, a function triggered by writes to a specific Realtime Database path that concludes by writing to that same path.\n\nSimplify asynchronous code with JavaScript promises\n\nPromises are a modern alternative to callbacks for asynchronous code. A promise\nrepresents an operation and the future value it may return. It also lets you\npropagate errors similar to try/catch in synchronous code. You can read about\npromises in the Firebase SDK on [The Firebase\nBlog](https://firebase.googleblog.com/2016/01/keeping-our-promises-and-callbacks_76.html),\nand promises in general on\n[MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).\n\nHow promises work with functions\n\nWhen you return a JavaScript promise to a function, that function\nkeeps running until the promise is resolved or rejected. To indicate that a\nfunction has completed its work successfully, the promise should be resolved. To\nindicate an error, the promise should be rejected. This means you only need to\nhandle errors that you want to.\n\nThe following code takes a Firebase Realtime Database `ref` and sets its value to\n`\"world!\"`. By returning the result of `set`, your function is guaranteed to\nkeep running until the asynchronous work of writing the string to the database\nis fully completed: \n\n // Always change the value of \"/hello\" to \"world!\"\n exports.hello = functions.database.ref('/hello').onWrite(event =\u003e {\n // set() returns a promise. We keep the function alive by returning it.\n return event.data.ref.set('world!').then(() =\u003e {\n console.log('Write succeeded!');\n });\n });\n\nExamples in context\n\nMost of our Cloud Functions [code\nsamples](https://github.com/firebase/functions-samples)\ninclude examples of proper function termination. Here are a few that demonstrate\ntypical cases:\n\n- [Realtime Database\n trigger](https://github.com/firebase/functions-samples/tree/main/Node-1st-gen/quickstarts/uppercase-rtdb/functions/index.js): an HTTP function followed by a redirect\n- [Cloud Storage\n trigger](https://github.com/firebase/functions-samples/tree/main/Node-1st-gen/quickstarts/thumbnails/functions/index.js): A storage download followed by `then`\n- [Webhook on Realtime Database\n write](https://github.com/firebase/functions-samples//tree/main/Node-1st-gen/minimal-webhook/functions/index.js): An error thrown inside a `then` clause\n- [Periodically delete unused\n accounts](https://github.com/firebase/functions-samples//tree/main/Node-1st-gen/delete-unused-accounts-cron/functions/index.js): A rejected promise"]]