'Javascript: How to specify http method with AWS Lambda.invoke()?

From AWS documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property

var params = {
   FunctionName: 'STRING_VALUE', /* required */
   ClientContext: 'STRING_VALUE',
   InvocationType: Event | RequestResponse | DryRun,
   LogType: None | Tail,
   Payload: Buffer.from('...') || 'STRING_VALUE',
   Qualifier: 'STRING_VALUE'
};
lambda.invoke(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
});

How do I specify http method, e.g. GET, POST, PUT, DELETE when calling lambda.invoke()?



Solution 1:[1]

  • We should add Flag as the useEffect dependency, otherwise useEffect won't know the Flag is changed to 1.

  • We should store the setInterval timer to Ref variable

       const timer = useRef();
       const [seconds, setSeconds] = useState(0);
       const [Flag, setFlag] = useState(0);
    
       function inc() {
         setSeconds((seconds) => seconds + 1);
         setFlag(1);
       }
    
       useEffect(() => {
         if (Flag === 1) {
           if (timer.current) {
             clearInterval(timer.current);
           }
         } else {
           timer.current = setInterval(inc, 1000);
         }
       }, [Flag]);
    

Solution 2:[2]

Why it is not stopping is simple: useEffect callback runs only once (because empty dependency array makes it behave like componentDidMount). When that is run Flag is 0, so clearInterval is not run.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gokulsgr
Solution 2 Tushar Shahi