title: Cloud functions Error: function crashed out of request scope Function invocation was interrupted
body: <p>I randomly get this error message when trying to send notification payloads to some devices.
  It will happen maybe 1 in every 10 successful attempts.</p>
  
  <p>I am a complete novice when it comes to cloud functions and node.js, Hoping its a silly simple mistake in my code that only sometimes seems to break.</p>
  
  <p>When searching this error message i find that its maybe down to function completing before the notification even sends. But i am unsure how to go about trying to fix it.</p>
  
  <p>Can anyone guide me in the right direction with the following function...</p>
  
  <pre><code>    *** // // --------------------- new chat message function -------------------
    //
    exports.newChatMessage = functions.firestore
    .document('/notifications/{toUID}/newMessage/{docID}')
    .onCreate((snap, context) =&gt; {
  
    const toUser = context.params.toUID;
    const documentID = context.params.docID;
    const newValue = snap.data();
    const title = newValue.title;
    const content = newValue.content;
    const fromUID = newValue.senderUID;
    const senderFirstName = newValue.senderFirstName;
    const deviceToken = newValue.deviceToken;
    const senderThumbUrl = newValue.senderThumbUrl;
    const bigPictureUrl = newValue.bigPictureUrl;
    const messageType = newValue.messageType;
    const time = newValue.time_stamp;
  
    console.log('New chat message to : ', toUser);
    console.log('Message ID : ', documentID);
    console.log('Type : ', messageType);
    console.log('Message : ', content);
    console.log('Time Stamp : ', time);
  
  
    const payload = {
      data : {
        title : title,
        content : content,
        senderUID : fromUID,
        senderThumbUrl : senderThumbUrl,
        senderFirstName : senderFirstName,
        bigPictureUrl : bigPictureUrl,
        messageType : messageType,
        notificationType : 'newChatMessage'
      }
    };
  
    admin.messaging().sendToDevice(deviceToken, payload).then(response =&gt; {
    console.log('Notification sent successfully');
  
    const deleteDoc = db.doc(`notifications/${toUser}/newMessage/${documentID}`);
    deleteDoc.delete();
    console.log('Notification document deleted');
  
    return true;
    })
    .catch(err =&gt; {
    console.log('Error : Problem sending notification', err);
    return false;
    });
    return true;
    });
    //
    // // ----------------- END OF :  new chat message function -------------- ***
  </code></pre>