اذهب إلى المحتوى
  • 0

حساب الوقت الذي تأخذه شيفرة async لتكتمل - React

Rayden Storm

السؤال


const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);

const next = async () => {
    setLoading(true);  

    updateCurrent(code)  //some async function
      .then(() => setLoading(false))  
      .catch((e) => {
        setLoading(false);
        setError(e);
      });
  };

هل هناك طريقة لجعل loading true فقط في حالة ما اذا updateCurrent أخذت أكثر من ثانية في العمل 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

يمكنك إستخدم setTimeout لتحقيق ذلك كالتالي

const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);

const next = async () => {
    const timer = setTimeout(() => {
      setLoading(true);
    }, 1000); 

    updateCurrent(code)  //some async function
      .then(() => {loading ? setLoading(false) : clearTimeout(timer);})  
      .catch((e) => {
        setLoading(false);
        setError(e);
      });
  };

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...