javascript
const BASE_URL = "/api/search";

export async function searchProducts(query, { signal } = {}) {
  const params = new URLSearchParams({ q: query, limit: "10" });
  const res = await fetch(`${BASE_URL}?${params}`, {
    signal,

Cancel Stale Autocomplete Requests with AbortController in a React Hook

react hooks abortcontroller
by codesnips 3 tabs
php
<?php

namespace App\Message;

final class SyncCustomerMessage
{

Retrying Flaky HTTP Calls with a Symfony Messenger Retry Strategy and Failure Handler

symfony messenger retry
by codesnips 3 tabs
rust
use async_trait::async_trait;
use std::io;

#[async_trait]
pub trait ConnectionFactory: Send + Sync + 'static {
    type Connection: Send + 'static;

Building a Bounded Async Database Connection Pool With Tokio Semaphore

rust tokio async
by codesnips 3 tabs
go
package feed

import "time"

type Event struct {
	ID        string    `json:"id"`

Stream and Decode Newline-Delimited JSON (NDJSON) from an HTTP Response Body in Go

go ndjson streaming
by codesnips 3 tabs
java
public class ProductAggregator implements AutoCloseable {

    private final RemoteServices services;
    private final ExecutorService pool = Executors.newFixedThreadPool(8);

    public ProductAggregator(RemoteServices services) {

Coordinate Parallel Remote Lookups With CompletableFuture in Java

java completablefuture concurrency
by codesnips 3 tabs
typescript
import { useCallback, useEffect, useReducer } from "react";

type State = { secondsLeft: number; isRunning: boolean };
type Action =
  | { type: "start"; seconds: number }
  | { type: "tick" }

Resend-Code Button With a Countdown Timer Using useEffect Cleanup

react hooks useeffect
by codesnips 3 tabs
ruby
module Retryable
  module_function

  def with_retries(tries: 3, base: 0.3, cap: 5.0, on: [StandardError])
    attempt = 0
    begin

Retry a Flaky HTTP Call with Exponential Backoff Using a Rails Service Object

rails http retries
by codesnips 3 tabs
java
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.Instant;

Nightly Cleanup of Expired Refresh Tokens with Spring @Scheduled and a Bulk Delete Query

spring-boot scheduling cron
by codesnips 4 tabs
php
<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\PrivateChannel;

Broadcasting Order Status Updates to a Private Channel with Laravel Echo

laravel broadcasting websockets
by codesnips 4 tabs
ruby
class ProjectPresenter
  def initialize(project, include_tasks: true)
    @project = project
    @include_tasks = include_tasks
  end

Building Nested JSON Presenters in Rails Without ActiveModel::Serializer

rails presenter json
by codesnips 3 tabs
java
package com.example.storage;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

Bind and Validate Grouped App Settings with @ConfigurationProperties in Spring Boot

spring-boot configuration validation
by codesnips 3 tabs
java
package com.shop.api.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

Version a Spring Boot REST API via Content Negotiation and Custom Media Types

spring-boot rest api-versioning
by codesnips 4 tabs