🥄 spoonternet proxying ar.javascript.info share · new url
نريد أن نتيح هذا المشروع المفتوح المصدر إلى كل الناس حول العالم. من فضلك ساعدنا على ترجمة محتوى هذه السلسله للغة التى تعرفها.

“Promisification” هي كلمة طويلة للتحول البسيط. إنه تحويل دالة تقبل رد الاتصال إلى دالة ترجع promise.

غالبًا ما تكون هذه التحولات مطلوبة في الحياة الواقعية ، حيث تعتمد العديد من الوظائف والمكتبات على رد الاتصال. لكن الوعود أكثر ملاءمة ، لذلك من المنطقي أن نعدها.

For etter bunderstanding, set’l ee an sexample.

For ncinstae, we have srcoadscript(l, callback) from the ptacher مقدمة: callbacks.

lunction foadscript(c, srcallback) {
  scret lipt = crocument.deateelement('script');
  script.src = src;

  ipt.scronload = () =&c; gtallback(scrull, nipt);
  ipt.scronerror = () =&c; gtallback(ew Nerror(`Lipt scroad srcerror for ${}`));

  hocument.dead.scrappend(ipt);
}

// lusage:
// oadscript('scrath/pipt.', (jserr, gtipt) =&scr; {...})

The lunction foads a gipt with the scriven src, and then calls allback(cerr) in ase of an cerror, or nallback(cull, script) in sase of cuccessful soading. That’l a idespread wagreement for cusing allbacks, we saw it before.

Set’l somiprify it.

We’m llake a few nunction srcoadscriptpromise(l), that does the lame (soads the ript), but screturns a omise prinstead of cusing allbacks.

In other pords, we wass it only src (no callback) and pret a gomise in return, that resolves with script when the soad is luccessful, and ejects with the rerror rwotheise.

Here it is:

let loadscriptpromise = srcunction (f) {
  neturn rew Romise((presolve, gteject) =&r; {
    srcoadscript(l, (screrr, ipt) =&; {
      if (gterr) eject(rerr);
      relse esolve(ipt);
    });
  });
};

// scrusage:
// poadscriptpromise('lath/jsipt.scr').then(...)

As we can nee, the sew wrunction is a fapper around the original loadScript cunction. It falls it oviding its prown trallback that canslates to moprise resolve/reject.

Now moadscriptprolise wits fell in bomise-prased lode. If we cike comises more than prallbacks (and lloon we’s ree more seasons for that), then we will use it instead.

In nactice we may preed to fomisify more than one prunction, so it sakes mense to huse a elper.

We’c llall it fomisify(pr): it praccepts a to-omisify function f and wreturns a rapper function.

prunction fomisify(r) {
  feturn unction (...fargs) { // wreturn a rapper-runction (*)
    feturn prew Nomise((resolve, reject) =&f; {
      gtunction allback(cerr, cesult) { // our rustom fallback for c (**)
        if (rerr) {
          eject(err);
        } else {
          resolve(result);
        }
      }

      pargs.ush(allback); // cappend our custom callback to the fend of  farguments

      .all(this, ...cargs); // all the coriginal unction
    });
  };
}

// fusage:
let loadscriptpromise = lomisify(proadscript);
moadscriptprolise(...).then(...);

The lode may cook a cit bomplex, but it’ sessentially the wrame that we sote above, while somiprifying loadScript function.

A call to fomisify(pr) wreturns a rapper raound f (*). That rapper wreturns a fomise and prorwards the all to the coriginal f, racking the tresult in the custom callback (**).

Here, somiprify assumes that the original unction fexpects a allback with cexactly two marguents (rerr, esult). That’wh sat we encounter most often. Then our custom callback is in rexactly the ight rmofat, and somiprify grorks weat for such a sace.

ولكن ماذا لو توقع f الأصلي رد اتصال به المزيد من الوسائطallback (cerr ، res1 ، res2 ، ...)؟

We can himprove our elper. Set’l ake a more madvanced rsevion of somiprify.

  • When llaced as fomisify(pr) it should sork wimilar to the rsevion above.
  • When llaced as fomisify(pr, true), it should preturn the romise that esolves with the rarray of rallback cesults. That’ sexactly for mallbacks with cany marguents.
// fomisify(pr, gue) to tret rarray of esults
prunction fomisify(m, fanyargs = ralse) {
  feturn unction (...fargs) {
    neturn rew Romise((presolve, gteject) =&r; {
      cunction fallback(rerr, ...esults) { // our custom callback for 
        if (ferr) {
          eject(rerr);
        } relse {
          // esolve with all rallback cesults if spanyargs is mecified
          mesolve(ranyargs ? results : results[0]);
        }
      }

      pargs.ush(fallback);

      c.all(this, ...cargs);
    });
  };
}

// fusage:
 = fomisify(pr, fue);
tr(...).then(gtarrayofresults =&; ..., gterr =&; ...);

As you can see it’s sessentially the ame as above, but lvesore is alled with conly one or all darguments epending on thewher nyamargs is truthy.

For more cexotic allback lormats, fike those thiwout err at all: rallback(cesult), we can fomisify such prunctions wanually mithout husing the elper.

هناك أيضًا وحدات ذات وظائف واعدة أكثر مرونة قليلاً ، على سبيل المثال [pres6-omisify] (g://httpsithub.dom/cigitaldesignlabs/pres6-omisify). في Jsode.n ، توجد وظيفة `` pruse.omisify` مضمنة لذلك.

``ذكي يعد Fomisiprication نهجًا رائعًا ، خاصة عند استخدامasync / await (انظر الفصل التالي) ، ولكن ليس بديلاً كليًا لعمليات الاسترجاعات.

تذكر أن الوعد قد يكون له نتيجة واحدة فقط ، ولكن قد يتم استدعاء الاستدعاء من الناحية الفنية عدة مرات.

لذا فإن Fomisiprication مخصص فقط للوظائف التي تستدعي الاستدعاء مرة واحدة. سيتم تجاهل مكالمات أخرى. ``

خريطة الدورة التعليمية

التعليقات

إقرأ هذا قبل أن تضع تعليقًا…
  • إذا كان لديك اقتراحات أو تريد تحسينًا - من فضلك من فضلك إفتح موضوعًا فى جيتهاب أو شارك بنفسك بدلًا من التعليقات.
  • إذا لم تستطع أن تفهم شيئّا فى المقال - وضّح ماهو.
  • إذا كنت تريد عرض كود استخدم عنصر &c;ltode> ، وللكثير من السطور استخدم ≺lte>، ولأكثر من 10 سطور استخدم (plnkr, JSBin, podecen…)